listView 有时候项目太少占用空间太大,如何能够自动控制
自适用大小
package com.customcontrols;
public class NoScrollListView extends ListView
{
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(MeasureSpec.UNSPECIFIED, 0) );
// here I assume that height's being calculated for one-child only, seen it in ListView's source which is actually a bad idea
int childHeight = getMeasuredHeight() - (getListPaddingTop() + getListPaddingBottom() + getVerticalFadingEdgeLength() * 2);
int fullHeight = getListPaddingTop() + getListPaddingBottom() + childHeight*(getCount());
setMeasuredDimension(getMeasuredWidth(), fullHeight);
}
}
本文介绍了一种自定义ListView的方法,通过重写onMeasure方法使ListView能够根据子项数量自动调整其高度,避免因项目过少而占用过多空间的问题。
1150

被折叠的 条评论
为什么被折叠?



