您的位置:首页 > 移动开发 > Android开发

Android ListView根据项数的大小自动改变高度

2012-06-18 19:43 846 查看
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);

setLayoutParams(new LayoutParams(50,android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: