Android: ListView in LinearLayout 让其他布局消失children

Android: ListView in LinearLayout let's other layout children disappear

我有一个简单的 LinearLayout 和一些 children。整个 activity 设计基本上是从 Google BluetoothChat 示例中复制的。 LinearLayout 包含 2 个 ListView,用于显示配对的蓝牙设备和可以找到的蓝牙设备(如果它们可被发现)。

问题:如果配对的设备太多,第一个ListView会占满整个屏幕,以至于ScanButton不再可见。不应该总是有足够的 space 用于 ScanButton,因为我正在使用 android:weight。

这是xml-code:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical"
>

<TextView
    android:id="@+id/title_paired_devices"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#666"
    android:paddingLeft="5dp"
    android:text="@string/deviceList_title_paired_devices"
    android:textColor="#fff"
    android:visibility="gone"
    />

<ListView
    android:id="@+id/paired_devices"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:stackFromBottom="true"
    />

<TextView
    android:id="@+id/title_new_devices"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#666"
    android:paddingLeft="5dp"
    android:text="@string/deviceList_title_other_devices"
    android:textColor="#fff"
    android:visibility="gone"
    />

<ListView
    android:id="@+id/new_devices"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="2"
    android:stackFromBottom="true"
    />

<Button
    android:id="@+id/button_scan"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Scan"
    />
 </LinearLayout>

您应该将 layout_weight 分配给所有视图,而不仅仅是 ListView

不要使用:

android:layout_height="wrap_content"

因为这会导致列表视图增长以适应所有内容。

为两个列表视图的高度和权重 1 设置 0dp:

android:layout_height="0dp"
android:layout_weight="1"