我遇到过这种情况,我在 RelativeLayout 中有 4 个小部件,顺序如下:
- TextView
- ListView
- TextView
- 按钮
这是布局的 XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/pedidoClienteRelativeLayout">
<TextView
android:id="@+id/nameLabel"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:maxLines="4"
android:text="Customer Name" />
<LinearLayout
android:id="@+id/ordersLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/nameLabel"
android:layout_above="@+id/totalOrdersLabel"
android:orientation="vertical" >
<ListView
android:id="@+id/booksListView"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:dividerHeight="1dp" >
</ListView>
</LinearLayout>
<TextView
android:id="@+id/totalOrdersLabel"
android:layout_above="@+id/confirmOrderButton"
android:layout_alignParentLeft="true"
android:text="Total" />
<Button
android:id="@+id/confirmOrderButton"
android:layout_alignParentBottom="true"
android:text="Confirm Order" />
</RelativeLayout>
这或多或少是结果:
如您所见,这是可行的。当我必须根据某些条件使 Button 不可见时,问题就出现了。然后 Button 上方的 TextView 无法控制使用 android:layout_above="@+id/confirmOrderButton"
进行引用,因此 textview 显示在 Layout 的顶部,而 ListView 只是未显示。
我认为如果我可以从第二个 TextView 中删除 android:layout_above="@+id/confirmOrderButton"
并添加 android:layout_alignParentBottom="true"
,那应该可以解决问题。问题是我完全不知道该怎么做,所以如果你能帮助我,我将不胜感激。
提前致谢。
最佳答案
将 totalOrdersLabel
TextView 的 android:layout_alignWithParentIfMissing
设置为 true。
<TextView
android:id="@+id/totalOrdersLabel"
android:layout_above="@+id/confirmOrderButton"
android:layout_alignParentLeft="true"
android:layout_alignWithParentIfMissing="true"
android:text="Total" />
关于android-layout - 如果位于该位置的小部件不可见,如何将小部件锚定到 Activity 的底部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26266144/