我正在尝试在 Android 中设置一个 Activity,其中 50% 的屏幕高度用于 ScrollView,另外 50% 用于 ListView。
为此,我将每个权重设置为“1”。
像这样:
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dip">
<ScrollView android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1">
<TableLayout android:id="@+id/my_table"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:shrinkColumns="0">
<TableRow android:padding="5dip"
android:id="@+id/fault_heading"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/table_title"
android:paddingRight="20dip"/>
</TableRow>
<!-- More rows are added at run time. -->
</TableLayout>
</ScrollView>
<ListView android:id="@+id/comments"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"></ListView>
</LinearLayout>
当 ScrollView 和 ListView 都有太多内容无法滚动显示时,这非常有效。
当 ScrollView 只需要 20% 的屏幕而 ListView 需要 70% 时,ListView 会增长到 70%,这样它就可以在不滚动的情况下显示。这不是我所期望的,但很好。
我遇到的问题是当 ListView 有太多数据时需要整个屏幕来显示,而 Scroll View 只有 1 或 2 行数据。在这种情况下,ListView 增长到几乎占满整个高度,并为 ScrollView 留下相当于大约一行文本的空间。
无论 ListView 有多大,我似乎无法理解如何为 ScrollView 保留一定比例的屏幕。
ScrollView 可以占用的空间量似乎与其子表中的行数成正比。当这仅为 1 时, ScrollView 的高度只有大约半厘米。
最佳答案
如何将 android:layout_height
更改为最小值而不是 0dip
?
更新:根据下面的评论,解决方案是将外部 LinearLayout 的高度设置为“fill_parent”
关于android - 防止 Android ListView 占据几乎所有屏幕高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5156314/