2012-04-24 109 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