设置 ListView 高度以显示所有可用项目
Set ListView height to display all available items
我在 ScrollView 中有一个 ListView。
如何设置 ListView 的高度,使其适配器中的所有项目都适合它并且不需要滚动。
如果您想要一个不滚动的项目列表,则称为线性布局。
否则,如果您愿意,可以将列表视图自定义为:
public class NonScrollListView extends ListView {
public NonScrollListView(Context context) {
super(context);
}
public NonScrollListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public NonScrollListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int heightMeasureSpec_custom = MeasureSpec.makeMeasureSpec(
Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom);
ViewGroup.LayoutParams params = getLayoutParams();
params.height = getMeasuredHeight();
}
}
您可以使用类似这样的方法,它会扩展列表,因此不需要滚动。请记住用 ScrollView:
将其包装在 XML 中
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content" >
和class:
public class ExpandedListView extends ListView {
private android.view.ViewGroup.LayoutParams params;
private int old_count = 0;
public ExpandedListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onDraw(Canvas canvas) {
if (getCount() != old_count) {
old_count = getCount();
params = getLayoutParams();
params.height = getCount() * (old_count > 0 ? getChildAt(0).getHeight() : 0);
setLayoutParams(params);
}
super.onDraw(canvas);
}
}
你也可以参考这个问题,这会有帮助:
Calculate the size of a list view or how to tell it to fully expand
如果您使用的是 ScrollView,如果内容的高度大于屏幕尺寸,它就会滚动。无论如何,尝试使用下面的参数,看看它是否符合您的要求。
android:layout_height="wrap_content"
我在 ScrollView 中有一个 ListView。
如何设置 ListView 的高度,使其适配器中的所有项目都适合它并且不需要滚动。
如果您想要一个不滚动的项目列表,则称为线性布局。
否则,如果您愿意,可以将列表视图自定义为:
public class NonScrollListView extends ListView {
public NonScrollListView(Context context) {
super(context);
}
public NonScrollListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public NonScrollListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int heightMeasureSpec_custom = MeasureSpec.makeMeasureSpec(
Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom);
ViewGroup.LayoutParams params = getLayoutParams();
params.height = getMeasuredHeight();
}
}
您可以使用类似这样的方法,它会扩展列表,因此不需要滚动。请记住用 ScrollView:
将其包装在 XML 中<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content" >
和class:
public class ExpandedListView extends ListView {
private android.view.ViewGroup.LayoutParams params;
private int old_count = 0;
public ExpandedListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onDraw(Canvas canvas) {
if (getCount() != old_count) {
old_count = getCount();
params = getLayoutParams();
params.height = getCount() * (old_count > 0 ? getChildAt(0).getHeight() : 0);
setLayoutParams(params);
}
super.onDraw(canvas);
}
}
你也可以参考这个问题,这会有帮助: Calculate the size of a list view or how to tell it to fully expand
如果您使用的是 ScrollView,如果内容的高度大于屏幕尺寸,它就会滚动。无论如何,尝试使用下面的参数,看看它是否符合您的要求。
android:layout_height="wrap_content"