1.定义:int totalHeight = 0;2.使用// 获取ListView对应的Adapter ListAdapter listAdapter = comment_list.getAdapter(); for (int i = 0, len = listAdapter.getCount(); i < len; i++) { // listAdapter.getCount()返回数据项的数目 View listItem = listAdapter.getView(i, null, comment_list); // 计算子项View 的宽高 listItem.measure(0, 0); // 统计所有子项的总高度 totalHeight += listItem.getMeasuredHeight(); } ViewGroup.LayoutParams params_comment = comment_list.getLayoutParams(); params_comment.height = totalHeight+ (comment_list.getDividerHeight() * (listAdapter.getCount() - 1)); // listView.getDividerHeight()获取子项间分隔符占用的高度 // params.height最后得到整个ListView完整显示需要的高度 comment_list.setLayoutParams(params_comment);3.注意:要是重新获取数据的话
一定要把这样的就不会出现空白的状况。totalHeight = 0;
android 计算listview的高度
最新推荐文章于 2024-08-15 17:51:07 发布