2011-09-19 37 views
1

我想創建以下佈局。Android:佈局與中間組件展開

+---------------------+ 
|  Text View  | 
+---------------------+ 
|      | 
|      | 
| Custom View  | 
| Expands to  | 
| extra space  | 
|      | 
+---------------------+ 
|Button1 |Button2 | 
+---------------------+ 

我的佈局xml如下所示。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <TextView android:text="This is test text." android:id="@+id/statusTxt" 
      android:layout_width="fill_parent" android:layout_height="wrap_content" /> 

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="vertical" android:layout_width="fill_parent" 
     android:layout_height="fill_parent" android:id="@+id/gameView" 
     android:layout_gravity="bottom"> 

     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:orientation="horizontal" android:layout_width="fill_parent" 
      android:layout_height="wrap_content" android:id="@+id/gameButtonView"> 

      <Button android:text="Done" android:id="@+id/doneBtn" 
       android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> 

      <Button android:text="Undo" android:id="@+id/undoBtn" 
       android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> 
     </LinearLayout> 
    </LinearLayout> 
</LinearLayout> 

比編程我加入我的自定義視圖

LinearLayout gameView = (LinearLayout)this.findViewById(R.id.gameView); 
gameView.addView(gameBoardView, 1); 

隨着當前佈局它結束了看起來像這樣。

+---------------------+ 
|  Text View  | 
+---------------------+ 
|Button1 |Button2 | 
+---------------------+ 
|      | 
|      | 
| Custom View  | 
| Expands to  | 
| extra space  | 
|      | 
+---------------------+ 

當前的佈局所做的是將最下面的按鈕粘到自定義視圖上方。關於如何讓它呈現我如何描述的任何想法?

+0

你能不能只需在中間的LinearLayout中取出android:layout_gravity =「bottom」? – brianestey

回答

6

使用RelativeLayout。將你的TextView設置爲alignParentTop =「true」。把你的按鈕與alignParentBottom一個的LinearLayout =「真」,然後設置在中間,上面=「按鈕」的內容和下方=「文本」

事情是這樣的:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <!-- Text at the top --> 
    <TextView 
     android:id="@+id/text" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" /> 

    <!-- Your game play view --> 
    <View 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_above="@+id/buttons" 
     android:layout_below="@+id/text" /> 

    <!-- Buttons at the bottom --> 
    <LinearLayout 
     android:id="@+id/buttons" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true"> 

     <Button 
      android:layout_weight="1" 
      android:layout_width="0dip" 
      android:layout_height="wrap_content" /> 

     <Button 
      android:layout_weight="1" 
      android:layout_width="0dip" 
      android:layout_height="wrap_content" /> 

    </LinearLayout> 

</RelativeLayout> 
+0

用你給出的例子,我如何以編程方式將該視圖元素設置爲我的自定義視圖?除此之外,我認爲你的答案將起作用。 – EpicOfChaos

+0

所以,這是與交易。你可以通過編程方式在LayoutParams中設置所有上面/下面的東西,但這是一個令人頭痛的問題。通過在該空間中放置一個帶有id的LinearLayout,然後用您的遊戲視圖替換該LinearLayout的內容(removeAllViews,然後在您希望交換新視圖的時候使用addView),我會走容易的路線。這樣你就不必對代碼中的LayoutParams進行任何操作......只需用FILL_PARENT,FILL_PARENT實例化它即可。 – Rich

+1

完美的作品,謝謝你的快速反應和詳細的例子。您是Android社區的偉大會員。 – EpicOfChaos