Android 动态获取ListView的高度

在代码中直接调用这个方法就可以了
 
 
public void setListViewHeightBasedOnChildren(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);
  }

你可能感兴趣的:(Android 动态获取ListView的高度)