01 | public void setListViewHeightBasedOnChildren(ListView listView) { |
02 | ListAdapter listAdapter = listView.getAdapter(); |
03 | if (listAdapter == null ) { |
08 | for ( int i = 0 ; i < listAdapter.getCount(); i++) { |
09 | View listItem = listAdapter.getView(i, null , listView); |
10 | listItem.measure( 0 , 0 ); |
11 | totalHeight += listItem.getMeasuredHeight(); |
14 | ViewGroup.LayoutParams params = listView.getLayoutParams(); |
15 | params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1 )); |
16 | ((MarginLayoutParams)params).setMargins( 10 , 10 , 10 , 10 ); |
17 | listView.setLayoutParams(params); |
02 | android:layout_width= "fill_parent" |
03 | android:layout_height= "fill_parent" |
04 | android:fadingEdge = "none" |
05 | android:background= "#FFF4F4F4" |
06 | xmlns:android= "http://schemas.android.com/apk/res/android" |
09 | android:gravity= "center_horizontal" |
10 | android:orientation= "vertical" |
11 | android:background= "#fff4f4f4" |
12 | android:layout_width= "fill_parent" |
13 | android:layout_height= "fill_parent" |
16 | android:id= "@+id/moreItemsListView" |
17 | android:layout_width= "fill_parent" |
18 | android:layout_height= "fill_parent" |
19 | android:cacheColorHint= "#FFF4F4F4" |
20 | android:dividerHeight= "0.0dip" |
21 | android:fadingEdge= "none" |
转载于:https://www.cnblogs.com/hudabing/p/3185880.html