zoukankan      html  css  js  c++  java
  • 【Android多屏适配】动态改变Listview item高度

    在ListView的Adapter中去直接获取传入View的LayoutParams是会报空指针异常的,唯一的方法是在xml中嵌套布局一层LinearLayout

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:background="@drawable/subject_item_bg"
        android:orientation="vertical" >
        <!-- 这层需要嵌套 -->    
        <LinearLayout
            android:id="@+id/subject_ll"
            android:layout_width="fill_parent"
            android:layout_height="120dp"
            android:orientation="vertical" >
    
            <ImageView
                android:id="@+id/subject_ico"
                android:layout_width="fill_parent"
                android:layout_height="0dp"
                android:layout_weight="17"
                android:layout_margin="5dp"
                android:scaleType="fitXY"
                android:src="@drawable/woji" />
    
            <TextView
                android:id="@+id/subject_title"
                android:layout_width="fill_parent"
                android:layout_height="0dp"
                android:layout_weight="5"
                android:layout_marginLeft="10dp"
                android:singleLine="true"
                android:text="曾经啊减肥的撒飞机 合集合集合集肥的撒飞机 合肥的撒飞机 合"
                android:textColor="#000000"
                android:textSize="18sp" />
        </LinearLayout>
    
    </LinearLayout>

    然后再在Adapter的getView中去动态改变即可,关键代码:

    if (view == null) {
                    view = getActivity().getLayoutInflater().inflate(
                            R.layout.subject_list_item, null);
                    holder = new ViewHolder();
                    LinearLayout ll = (LinearLayout)view.findViewById(R.id.subject_ll);
                    holder.tvTitle = (TextView) view
                            .findViewById(R.id.subject_title);
                    holder.imgIco = (ImageView) view.findViewById(R.id.subject_ico);
    
                    LayoutParams linearParams = (LayoutParams) ll
                            .getLayoutParams();
                    linearParams.height = screenWidth * 250 / 460;
    //                linearParams.gravity = Gravity.CENTER_VERTICAL;
                    ll.setLayoutParams(linearParams);
                    view.setTag(holder);
                } else {
                    holder = (ViewHolder) view.getTag();
                }
  • 相关阅读:
    fpga配置方式 .jic固化为ps模式
    fpga新建nios
    四轴飞行器飞行原理与双闭环PID控制
    fpga为什么要用nios 开发
    error A space is required after ',' comma-spacing
    vuex : Newline required at end of file but not found eol-last
    vue -Missing space before value for key 'path'vue.js解决空格报错
    visual studio 自动补全功能 以及代码没有颜色
    hadoop 伪分布模式环境搭建
    django框架-DRF工程之认证功能
  • 原文地址:https://www.cnblogs.com/zhujiabin/p/5806281.html
Copyright © 2011-2022 走看看