在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();            }

更多相关文章

  1. 解决NestedScrollView 嵌套 RecyclerView出现的卡顿,上拉刷新无效
  2. Android(安卓)NestedScrolling机制
  3. Android(安卓)scrollview嵌套listview出现listview无法滑动冲突
  4. fragment内嵌fragment之间传值+切换fragment
  5. Android支付Demo运行大坑
  6. android studio 中实现android全屏 AppCompat does not support
  7. 【FF7】关于多成Layer嵌套的问题
  8. 2014.03.07 ——— android GridView 记录和恢复位置
  9. 使用ViewServer让Android真机可用HierarchyView视图检测工具

随机推荐

  1. The Saygus VPhone V1 clears FCC, Will
  2. 浅析Android中的消息机制
  3. Android(安卓)Intent中的FLAG,很全
  4. Android客户端自动更新代码
  5. Android(安卓)对UI操作的工具类UIUtils
  6. android 客户端 smtp 协议发送数据
  7. Android获取单选与复选框的值
  8. android 判断文件是否存在
  9. Android之MediaPlayer(两种)基本使用方式
  10. C#内置泛型委托之Func委托