【发布时间】:2015-11-29 20:46:10
【问题描述】:
我有我认为可以直接实现的东西:一个带有两个 ListView 的线性布局,两个 ListView 的 layout_weight="1"。第一个是“填充父级”,第二个是“包装内容”。这个想法是,当第二个 ListView 为空时,第一个 ListView 将占据整个屏幕,并且当两个 ListView 都有条目时,屏幕将被共享。好吧,它几乎可以工作,但是当第二个 ListView 为空时,第一个 ListView 仅向下滚动到 LAST 条目的一半。最后一个条目的其余部分不在屏幕上。有谁明白为什么会这样?
这是我的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/dots"
android:layout_width="match_parent"
android:layout_height="24dp" >
</LinearLayout>
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/lni_logo" />
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:choiceMode="none"
android:transcriptMode="alwaysScroll"
android:layout_weight="1"
android:id="@android:id/list" >
</ListView>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:choiceMode="none"
android:transcriptMode="alwaysScroll"
android:layout_weight="1"
android:id="@+id/rtsalist">
</ListView>
</LinearLayout>
实际上在这个阶段我还没有为我的第二个 ListView 制作适配器,所以我无法使用其中的条目对其进行测试。但关键是,当它为空时,它应该就像我根本没有 ListView 一样。唉,事实并非如此。当我删除第二个 ListView 时,第一个会正确向下滚动到最后一个元素,显示整个条目。
感谢您的任何见解!
【问题讨论】:
-
fill_parent 和 match_parent 是一回事。仅在不推荐使用 fill_parent 时使用 match_parent。
-
您已经为您的 ListView 设置了 layout_weight,但是您需要将这两个 ListView 包装在一个新的 LinearLayot 中方向设置为
vertical,并且新的LinearLayout需要将android:weightSum属性设置为2,所以它们各占50%。 -
@Gi0rgi0s 如果其中一个是空的,那一个会占用任何空间吗?我此时所做的是以编程方式将空的设置为消失(rtsaList.setVisibility(View.GONE))。它似乎有效,但我没有尝试过使用条目。当然,当我这样做时,我有第二个列表视图的 layout_height="match_parent"。
-
您还必须以编程方式将布局权重更改为 2。您这样做的方式是非标准的。
-
好的;很高兴知道。帮助我更好地理解布局权重。
标签: android listview android-linearlayout