方法一,最直接的方法就是刪除 ScrollView控件java
方法二:寫個工具類動態設置listview的高度ide
public class ListViewUtil { /** * 動態設置ListView的高度 * * @param listView */ public static void setListViewHeight(ListView listView) { if (listView == null) return; 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); } }
而後使用的時候工具
ListViewUtil.setListViewHeight(listView); listview.setAdapter(adapter);