Android有许多相对位置/堆栈的备选方案

Android有许多相对位置/堆栈的备选方案,android,layout,Android,Layout,我必须将视图A放置在视图B的顶部,将视图B放置在视图C的顶部 在可见性更改(消失)或元素删除时,其行为必须类似于堆栈。如果我移除B,A必须位于C的顶部,如果我移除C,B将位于父级的底部,A保持在B的上方,如果我移除B和C,A将位于父级的底部 目前我有以下属性: C: B: A: 但是我需要像“如果有B,在上面对齐,如果没有,在C的上面对齐,如果没有对齐父对象的底部” 有没有办法在没有嵌套布局的XML中解决这个问题?您可以不使用RelativeLayout,而是使用第四个视图的线性布局,布局权重

我必须将视图A放置在视图B的顶部,将视图B放置在视图C的顶部

在可见性更改(消失)或元素删除时,其行为必须类似于堆栈。如果我移除B,A必须位于C的顶部,如果我移除C,B将位于父级的底部,A保持在B的上方,如果我移除B和C,A将位于父级的底部

目前我有以下属性:

C:

B:

A:

但是我需要像“如果有B,在上面对齐,如果没有,在C的上面对齐,如果没有对齐父对象的底部”


有没有办法在没有嵌套布局的XML中解决这个问题?

您可以不使用RelativeLayout,而是使用第四个视图的线性布局,布局权重设置为1

<?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" >

    <View
        android:id="@+id/spacer"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="#000000" />

    <View
        android:id="@+id/A"
        android:layout_width="fill_parent"
        android:layout_height="30dip"
        android:background="#FF0000" />

    <View
        android:id="@+id/B"
        android:layout_width="fill_parent"
        android:layout_height="30dip"
        android:background="#00FF00" />

    <View
        android:id="@+id/C"
        android:layout_width="fill_parent"
        android:layout_height="30dip"
        android:background="#0000FF" />

</LinearLayout>


您可以使用alignParentBottom=“true”@njzk2:Agreed将linearlayout放在relativelayout中,而不是第四个视图。与上面的答案相同,但删除“间隔”视图,使线性布局alignParentBottom=“true”,并将线性布局的布局高度设置为WRAP\u content为获得更好的性能,最好水平增加层次视图,而不是垂直增加层次视图,这就是我提出此解决方案的原因,但是,你的解决方案也有效。这是一种解决方法。但现在我有一个案例,我想它是不可用的。我必须使用相对布局,因为我想把底部的项目放在屏幕上所有其他项目的前面。我不能像你所说的那样使用上面的第四个布局将它们向下推,因为它会隐藏/消耗我后面实际布局中的事件。不完全,请参见我在RorolePro的回答中的最后一条评论。
android:id="@+id/B"
android:layout_above="@id/C"
android:layout_alignWithParentIfMissing="true"
android:layout_above="@id/B"
android:layout_alignWithParentIfMissing="true"
<?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" >

    <View
        android:id="@+id/spacer"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="#000000" />

    <View
        android:id="@+id/A"
        android:layout_width="fill_parent"
        android:layout_height="30dip"
        android:background="#FF0000" />

    <View
        android:id="@+id/B"
        android:layout_width="fill_parent"
        android:layout_height="30dip"
        android:background="#00FF00" />

    <View
        android:id="@+id/C"
        android:layout_width="fill_parent"
        android:layout_height="30dip"
        android:background="#0000FF" />

</LinearLayout>