在item的layout文件中,用android:layout_height设置item的高度。
运行,高度设置无效。
解决办法:
给item设定minHeight,即可.
---------------------------------------
最近一直纠结于ListView中每个Item中高度的问题,在网上只找出一个方法,就是在每个item里面加入图片,也就是ImageView,这样用图片来“撑”它的高度,这样实在是费劲,不可也可以解决问题
今天无意间看SDK里面的Demo,发现还有一种方法,如下黑色粗体所示
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:minHeight="?android:attr/listPreferredItemHeight"
>
<TextView
android:id="@+id/tv"
android:layout_width="fill_parent"
android:layout_height="20dp"
android:text="@string/hello"
/>
</LinearLayout>
根据数据设置listView的高度
public void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
return;
}
int totalHeight = 0;
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight
+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
((MarginLayoutParams) params).setMargins(10, 10, 10, 10);
listView.setLayoutParams(params);
}
<ListView
android:id="@+id/getInfo"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="#FFF4F4F4"
android:dividerHeight="0.0dip"
android:fadingEdge="none"
/>