2013-04-18 64 views
0

我想知道如何使用的RelativeLayout爲了使這樣的tructure:的Android RelativeLayout的兩個按鈕rightof一個按鈕

三個按鈕,我們姑且稱之爲A B和C;我想把B和C放在另一個的上面,把它們放在A的右邊,作爲一個匹配A的寬度和高度的單個塊(所以我們將B和C的高度設爲A的一半,但是相同的寬度)。

因爲我還不能在這裏發表圖片,你可以找到我要找的圖像:http://img191.imageshack.us/img191/9765/stackg.jpg

我寫了一篇關於這部分的代碼是這一個:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@android:color/black" 
    android:paddingLeft="16dp" 
    android:paddingRight="16dp" > 

    <Spinner 
     android:id="@+id/A" 
     android:layout_width="0dp" 
     android:layout_height="96dp" 
     android:layout_below="@id/another_block_that_fills_all_the_line" 
     android:layout_alignParentLeft="true" 
     android:layout_toLeftOf="@+id/B" /> 

    <Spinner 
     android:id="@id/B" 
     android:layout_width="96dp" 
     android:layout_height="48dp" 
     android:layout_below="@id/another_block_that_fills_all_the_line" 
     android:layout_alignParentRight="true" /> 

    <Spinner 
     android:id="@id/C" 
     android:layout_width="96dp" 
     android:layout_height="48dp" 
     android:layout_below="@id/B" 
     android:layout_alignParentRight="true" /> 

</RelativeLayout> 

的問題在於它不會隨着每個屏幕而擴展,因爲我必須設置靜態大小。我嘗試了很多不同的解決方案,比如對layout_align使用所有可能的組合,甚至隨機更改wrap_content,但仍然無法按照我希望它們使用靜態大小的方式來適應它們。

+0

我已經更新我的答案。覈實。我認爲這正是你需要的 –

回答

1

使用LinearLayoutlayout_weight。試試這個佈局

<LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" 
    android:orientation="horizontal"> 

    <Button android:layout_width="0dip" android:layout_height="fill_parent" 
     android:layout_weight="1.0"/> 

    <LinearLayout android:layout_width="0dip" android:layout_height="fill_parent" 
     android:layout_weight="1.0" android:orientation="vertical"> 

     <Button android:layout_width="fill_parent" android:layout_height="0dip" 
      android:layout_weight="1.0"/> 

     <Button android:layout_width="fill_parent" android:layout_height="0dip" 
      android:layout_weight="1.0"/> 

    </LinearLayout> 
</LinearLayout> 
+0

嵌套layout_weight影響性能 –

0

試試這個,

<?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:background="@android:color/black"> 
    <Button 
     android:id="@+id/A" 
     android:layout_width="wrap_content" 
     android:layout_centerInParent="true" 
     android:layout_height="wrap_content" 
     android:layout_alignTop="@+id/relativeLayout" 
     android:layout_alignBottom="@+id/relativeLayout" 
     android:text="AAA"/> 
    <RelativeLayout 
     android:id="@+id/relativeLayout" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@+id/A" 
     android:layout_centerVertical="true" 
     android:background="@android:color/black"> 
     <Button 
      android:id="@+id/B" 
      android:layout_width="wrap_content" 
      android:text="BBB" 
      android:layout_height="wrap_content"/> 
     <Button 
      android:id="@+id/C" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@id/B" 
      android:text="CCC"/> 
    </RelativeLayout> 
</RelativeLayout> 
0

它應該是如下

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:gravity="center_horizontal" 
android:padding="10dp" > 
<View 
    android:id="@+id/helper" 
    android:layout_width="0dp" 
    android:layout_height="0dp" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" /> 
<Spinner 
    android:id="@+id/A" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_toLeftOf="@id/helper" 
    android:background="@android:color/black" /> 
<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignBottom="@id/A" 
    android:layout_alignParentRight="true" 
    android:layout_alignTop="@id/A" 
    android:layout_marginLeft="10dp" 
    android:layout_toRightOf="@id/helper" 
    android:orientation="vertical" > 
    <Spinner 
     android:id="@+id/B" 
     android:layout_width="wrap_content" 
     android:layout_height="0dp" 
     android:layout_marginBottom="2dp" 
     android:layout_weight="1" 
     android:background="@android:color/black" /> 
    <Spinner 
     android:id="@+id/C" 
     android:layout_width="wrap_content" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:background="@android:color/black" /> 
</LinearLayout>