ListView 是安卓中很常见的空间。
不过它的高度,有时候很难去控制。
如果布局中有 <ScrollView /> 把listView 包括起来。那它的高度就会变成一个单元的高度。
解决办法

 /**
  * 设置列表框高度,解决滚屏问题
  */
private void initListViewHeight(ListView list){
 ProductListAdapter listAdapter = (ProductListAdapter)list.getAdapter();
 if (listAdapter == null || listAdapter.getCount() == 0) {
  ViewGroup.LayoutParams params = list.getLayoutParams();
  params.height = 0;
  list.setLayoutParams(params);
     return; 
 }
 int totalHeight = 0; 
 for (int i = 0; i < listAdapter.getCount(); i++) { 
     View listItem = listAdapter.getView(i, null, list); 
     listItem.measure(0, 0); 
     totalHeight += listItem.getMeasuredHeight(); 
 }  
 ViewGroup.LayoutParams params = list.getLayoutParams(); 
 params.height = totalHeight + (list.getDividerHeight() * (listAdapter.getCount() - 1)); 
 params.height += 5;
 list.setLayoutParams(params); 
}
      

相关文章:

  • 2021-05-20
  • 2021-04-12
  • 2021-09-29
  • 2022-12-23
  • 2022-12-23
  • 2021-12-09
  • 2021-08-13
  • 2022-12-23
猜你喜欢
  • 2022-01-09
  • 2021-07-13
  • 2021-12-06
  • 2021-06-07
  • 2021-12-24
  • 2021-11-15
  • 2022-12-23
相关资源
相似解决方案