/** * @param 自动匹配listview的高度 */ public void setListviewHeight(ListView listView) { ListAdapter listAdapter = listView.getAdapter(); if (listAdapter == null) { return; } int totalHeight = 0; for (int i = 0; i < listAdapter.getCount(); i++) { View listitemView = listAdapter.getView(i, null, listView); listitemView.measure(0, 0); totalHeight += listitemView.getMeasuredHeight(); } ViewGroup.LayoutParams params = listView.getLayoutParams(); params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)); ((MarginLayoutParams) params).setMargins(0, 0, 0, 0); listView.setLayoutParams(params); }
时间: 2024-11-05 22:05:49