2011-10-28 111 views
0

我是Android新手,所以不太擅長GUI佈局。我正在爲我的應用程序構建一個GUI,但無法讓它按照我想要的方式行事。我需要的是屏幕底部水平堆疊的4個按鈕。在按鈕上方放置了一個SurfaceView,我想填充屏幕的其餘部分。結果應該是這樣的(我希望這是明確的):
Android - 圖形用戶界面佈局

----------------- 
-    - 
-    - 
-    - 
-    - 
- SurfaceView - 
-    - 
-    - 
-    - 
----------------- 
--- --- --- --- 
-B- -B- -B- -B- 
--- --- --- --- 

我得到的closeset是這樣的:

<?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" > 
    <TableLayout 
     android:id="@+id/tableLayout1" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" > 
     <TableRow 
      android:id="@+id/tableRow1" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:gravity="center" > 
      <SurfaceView 
       android:id="@+id/surfaceView1" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" /> 
     </TableRow> 
     <TableRow 
      android:id="@+id/tableRow2" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:gravity="center" > 
      <LinearLayout 
       android:id="@+id/linearLayout1" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" > 
       <Button 
        android:id="@+id/button2" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_margin="5dp" 
        android:text="Button" /> 
       <Button 
        android:id="@+id/button3" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_margin="5dp" 
        android:text="Button" /> 
       <Button 
        android:id="@+id/button4" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_margin="5dp" 
        android:text="Button" /> 
       <Button 
        android:id="@+id/button1" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_margin="5dp" 
        android:text="Button" /> 
      </LinearLayout> 
     </TableRow> 
    </TableLayout> 
</LinearLayout> 

不幸的結果是這樣的:

----------------- 
- SurfaceView - 
----------------- 
--- --- --- --- 
-B- -B- -B- -B- 
--- --- --- --- 

回答

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


      <SurfaceView 
       android:id="@+id/surfaceView1" 
       android:layout_width="fill_parent" 
       android:layout_height="0dp" 
       android:layout_weight = "1" 
      /> 

      <LinearLayout 
       android:id="@+id/linearLayout1" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" > 
       <Button 
        android:id="@+id/button2" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_margin="5dp" 
        android:text="Button" /> 
       <Button 
        android:id="@+id/button3" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_margin="5dp" 
        android:text="Button" /> 
       <Button 
        android:id="@+id/button4" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_margin="5dp" 
        android:text="Button" /> 
       <Button 
        android:id="@+id/button1" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_margin="5dp" 
        android:text="Button" /> 
      </LinearLayout> 

</LinearLayout> 

試試這個。

layout_weight是一個工作在線性佈局的孩子上的標籤。如果提到的話,孩子們會根據體重分享取向測量。在你的情況下,由於線性佈局是垂直的,因此如果提到權重,他們將共享高度。

linearLayout的默認weightSum是1,所以如果你給任何一個孩子1,它會佔用剩餘的空間。假設父母有2個孩子,其體重爲0.6和0.4,第一個孩子將佔60%的父母身高,其餘40%由第二個孩子。

+0

謝謝,這工作。只要網站允許,我會盡快接受。你可以請解釋你如何使用這些屬性:android:layout_height =「0dp」 android:layout_weight =「1」 – Yoav

+1

檢查我編輯的答案。 –

+0

再次感謝 – Yoav

0

嘗試對頂層佈局使用相對佈局,並將alignParentBottom用於按鈕欄佈局。