动态设定GridView的高度,固定column,根据gridview中的item个数设定高度:
调用以下方法:
- public static void setListViewHeightBasedOnChildren(GridView listView) {
-
- ListAdapter listAdapter = listView.getAdapter();
- if (listAdapter == null) {
- return;
- }
-
- int col = 4;
- int totalHeight = 0;
-
-
- for (int i = 0; i < listAdapter.getCount(); i += col) {
-
- View listItem = listAdapter.getView(i, null, listView);
- listItem.measure(0, 0);
-
- totalHeight += listItem.getMeasuredHeight();
- }
-
-
- ViewGroup.LayoutParams params = listView.getLayoutParams();
-
- params.height = totalHeight;
-
- ((MarginLayoutParams) params).setMargins(10, 10, 10, 10);
-
- listView.setLayoutParams(params);
- }
调用此方法后,需要在调用notifyDataSetChanged()方法,实现界面刷新
Android动态设定GridView的高度,固定column,实现高度自适应
原文:http://www.cnblogs.com/Free-Thinker/p/4931121.html