public static void setListViewHeight(ListView listView)
{
//获取ListView对应的Adapter
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();
}
ViewGounp.LayoutParams params=listView.getLayoutParams();
params.height=totalHeight+
(listView.getDividerHeight()*(listAdapter.getCount()-1));
listView.setLayoutParams(params);
}