gpt4 book ai didi

android - 如何在 ScrollView 中完全实现 ListView

转载 作者:太空宇宙 更新时间:2023-11-03 10:43:46 25 4
gpt4 key购买 nike

在这里,我在 ScrollView 中使用了 listView,一切正常,但是当我设置时 listView 没有完全显示它的高度fill_parent。所以我固定了它的高度,当我更换安卓手机设备时它看起来不太好。任何人都可以帮助我吗? 我也发布了我的 xml 类(class)。

演示.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="25dp"
android:gravity="center"
android:orientation="vertical" >

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingLeft="6dp"
android:text="@string/course_tracker"
android:textStyle="bold" />
</LinearLayout>

<LinearLayout
android:id="@+id/ll_finishapp_table"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:orientation="vertical" >

<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#E3E3E3" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:orientation="horizontal"
android:weightSum="20" >

<TextView
android:layout_width="2dp"
android:layout_height="match_parent"
android:background="#E3E3E3"
android:text="" />

<LinearLayout
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="5"
android:orientation="vertical" >

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal|center_vertical"
android:text="@string/program_name" />
</LinearLayout>

<TextView
android:layout_width="2dp"
android:layout_height="match_parent"
android:background="#E3E3E3"
android:text="" />

<LinearLayout
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="2"
android:gravity="center"
android:orientation="vertical"
android:background="#e9e9e9" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/colon"
android:textStyle="bold" />
</LinearLayout>

<TextView
android:layout_width="2dp"
android:layout_height="match_parent"
android:background="#E3E3E3"
android:text="" />

<LinearLayout
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="13"
android:gravity="center_vertical"
android:orientation="vertical" >

<TextView
android:id="@id/tv_program_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:textStyle="bold" />
</LinearLayout>

<TextView
android:layout_width="2dp"
android:layout_height="match_parent"
android:background="#E3E3E3"
android:text="" />
</LinearLayout>

<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#E3E3E3" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#F9F9F9"
android:orientation="horizontal"
android:weightSum="20" >

<TextView
android:layout_width="2dp"
android:layout_height="match_parent"
android:background="#E3E3E3"
android:text="" />

<LinearLayout
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="5"
android:orientation="vertical" >

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal|center_vertical"
android:text="@string/program_description" />
</LinearLayout>

<TextView
android:layout_width="2dp"
android:layout_height="match_parent"
android:background="#E3E3E3"
android:text="" />

<LinearLayout
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="2"
android:gravity="center"
android:orientation="vertical"
android:background="#e9e9e9" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/colon"
android:textStyle="bold" />
</LinearLayout>

<TextView
android:layout_width="2dp"
android:layout_height="match_parent"
android:background="#E3E3E3"
android:text="" />

<LinearLayout
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="13"
android:gravity="center_vertical"
android:orientation="vertical" >

<TextView
android:id="@id/tv_program_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:textStyle="bold" />
</LinearLayout>

<TextView
android:layout_width="2dp"
android:layout_height="match_parent"
android:background="#E3E3E3"
android:text="" />
</LinearLayout>

<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#E3E3E3" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp"
android:orientation="vertical" >

<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#E3E3E3" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="30dp"
android:background="#ffffff"
android:orientation="horizontal"
android:weightSum="20" >

<TextView
android:layout_width="2dp"
android:layout_height="match_parent"
android:background="#E3E3E3"
android:text="" />

<LinearLayout
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="10"
android:orientation="vertical" >

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal|center_vertical"
android:text="@string/course_name" />
</LinearLayout>

<TextView
android:layout_width="2dp"
android:layout_height="match_parent"
android:background="#E3E3E3"
android:text="" />

<LinearLayout
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="10"
android:orientation="vertical" >

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal|center_vertical"
android:text="@string/semester" />
</LinearLayout>

<TextView
android:layout_width="2dp"
android:layout_height="match_parent"
android:background="#E3E3E3"
android:text="" />
</LinearLayout>

<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#E3E3E3" />

<ListView
android:id="@id/list_course"
android:layout_width="match_parent"
android:layout_height="350dp"
android:layout_marginBottom="10dp" >
</ListView>
</LinearLayout>

</LinearLayout></ScrollView>

谢谢。

最佳答案

根据列表个数动态设置ListView的高度,比如:

private 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);
if (listItem != null) {
listItem.measure(
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(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 后调用上述方法,例如:

urListView.setAdapter(urAdapter);
setListViewHeightBasedOnChildren(urListView);

关于android - 如何在 ScrollView 中完全实现 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28689000/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com