动态改变listview的高度

来源:互联网 发布:maxwell软件 百科 编辑:程序博客网 时间:2024/05/21 05:44
<span style="color:#ff0000;">Utiltools.setListViewHeightBasedOnChildren(lv);//使用的时候</span>
public static void setListViewHeightBasedOnChildren(ListView listView) {ListAdapter listAdapter = listView.getAdapter();if (listAdapter == null) {return;}int totalHeight = 0;for (int i = 0, len = listAdapter.getCount(); i < len; i++) {View listItem = listAdapter.getView(i, null, listView);listItem.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));listItem.measure(0, 0);totalHeight += listItem.getMeasuredHeight();}ViewGroup.LayoutParams params = listView.getLayoutParams();params.height = totalHeight+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));listView.getDividerHeight();listView.setLayoutParams(params);}


0 0
原创粉丝点击