用Adapter自定义ListView的显示,在ScrollView下只能显示一行
通过重算高度显示后发现,如果TextView中有换行,那么高度计算会不准确。
解决办法据说是重写ListView的OnMeasure方法或者自定义ListItem.xml
不过直接去掉了上一层ScrollView就可以了,ListView支持滚动条。
public static 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)); listView.setLayoutParams(params); }