public static voID setListVIEwHeightBasedOnChildren(ListVIEw ListVIEw) { 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,MeasureSpec.UnspecIFIED); totalHeight += ListItem.getMeasuredHeight(); } VIEwGroup.LayoutParams params = ListVIEw.getLayoutParams(); params.height = totalHeight + (ListVIEw.getdivIDerHeight() * (listadapter.getCount() - 1)); ListVIEw.setLayoutParams(params); ListVIEw.requestLayout(); }
但它并没有给出实际的高度;它为所有列表项赋予相同的高度.可以找到具有不同高度的列表项的ListVIEw高度?
解决方法private int getTotalHeightofListVIEw() { listadapter LvAdapter = lv.getAdapter(); int ListvIEwElementsheight = 0; for (int i = 0; i < mAdapter.getCount(); i++) { VIEw mVIEw = mAdapter.getVIEw(i,lv); mVIEw.measure( MeasureSpec.makeMeasureSpec(0,MeasureSpec.UnspecIFIED),MeasureSpec.makeMeasureSpec(0,MeasureSpec.UnspecIFIED)); ListvIEwElementsheight += mVIEw.getMeasuredHeight(); } return ListvIEwElementsheight;}
尝试这个代码.
总结以上是内存溢出为你收集整理的如何在android中获取listview高度?全部内容,希望文章能够帮你解决如何在android中获取listview高度?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)