【Android多屏适配】动态改变Listview item高度
2014-03-25 22:45
344 查看
在ListView的Adapter中去直接获取传入View的LayoutParams是会报空指针异常的,唯一的方法是在xml中嵌套布局一层LinearLayout
然后再在Adapter的getView中去动态改变即可,关键代码:
<?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(); }
相关文章推荐
- 【Android多屏适配】动态改变Listview item高度
- 【Android多屏适配】动态改变Listview item高度
- XIB创建的cell中的已经适配过的控件如何动态改变高度?
- Android-Dialog根据listview的数据量动态改变高度,并且有一个最大的高度
- Android动态改变ListView的高度
- Android 动态改变高度以及计算长度的EditText
- Android动态改变高度以及计算长度的EditText
- Android实现图片宽度全屏,高度随图片大小动态适配
- Android 使用动画来动态改变View的高度
- Android动态改变ListView的高度
- 【android】RecyclerView中动态添加子控件和改变ViewHolder高度
- IOS 使用Autolayout实现UITableView的Cell动态布局和高度动态改变
- 浏览器窗口大小改变时,动态改变div高度和宽度
- android editText动态改变事件
- Android 利用 <activity-alias> 动态改变 App 桌面图标
- Android-UI详解:软键盘弹出后,动态改变UI布局(IM聊天窗口设计示例)
- 动态的改变IFrame的高度
- Android中使用selector动态改变imageView或ImageButton的背景和在GridView中应用
- Android ListView根据项数的大小自动改变高度
- 使用Autolayout实现UITableView的Cell动态布局和高度动态改变