2012-04-24 141 views
0

的Android许多替代我不得不上视图C.的顶部为相对位置/堆栈

具有表现得像上可见性改变(消失)或元件移除堆栈上视图B和B的顶部位置视图甲。如果我删除B,则A必须位于C的顶部,如果我删除C,则B将移到父代的底部,并且A保持在B的上方,如果删除B和C,则A将移到父代的底部。

enter image description here

目前我有这个属性:

C:

android:id="@+id/C" 
android:layout_alignParentBottom="true" 

B:

android:id="@+id/B" 
android:layout_above="@id/C" 
android:layout_alignWithParentIfMissing="true" 

答:

android:layout_above="@id/B" 
android:layout_alignWithParentIfMissing="true" 

但我需要的东西像是“如果有B,在它上面对齐,如果没有,在C顶部对齐,如果不对齐父级的底部”。

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

+0

一个layout_weight您的问题是否得到解决? – 2012-04-26 03:37:55

+0

不完整,请参阅我在RorolePro的回答中的最新评论。 – Ixx 2012-06-15 15:21:34

回答

1

你可以做的,而不是使用RelativeLayout的是什么,是使用的LinearLayout与第四视图设置为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> 
+0

而不是第四个视图,你可以把你的线性布局放在与alignParentBottom =“true” – njzk2 2012-04-24 15:43:36

+0

@ njzk2相关的布局中:同意。与上面的答案相同,但删除“Spacer”视图,使线性布局alignParentBottom =“true”,并将LinearLayout的layout_height设置为WRAP_CONTENT – Gophermofur 2012-04-24 16:09:28

+0

为了获得更好的性能,最好水平增加层级视图而不是垂直增量,这就是为什么我提出这个解决方案,但是,你的解决方案也可以。 – 2012-04-25 07:43:33