动态设置ListView的高度
public void setListViewHeightBasedOnChildren(ListView mListView) { ListAdapter listAdapter = mListView.getAdapter(); if (listAdapter == null) { return; } int totalHeight = 0; for (int i = 0; i < listAdapter.getCount(); i++) { View listItem = listAdapter.getView(i, null, mListView); listItem.measure(0, 0); totalHeight += listItem.getMeasuredHeight(); } ViewGroup.LayoutParams params = mListView.getLayoutParams(); params.height = totalHeight + (mListView.getDividerHeight() * (listAdapter.getCount() - 1)); ((MarginLayoutParams) params).setMargins(10, 10, 10, 10); mListView.setLayoutParams(params); }
解决的ScrollView嵌套ListView只显示一行的问题
时间: 2024-11-05 21:06:47