2014-02-27 129 views
5

我試圖創建一個佈局,其中包含一個2x2的按鈕網格(共4個)。我得到了以下內容,但它只在左上角創建按鈕網格。我想讓按鈕網格填滿整個屏幕。2x2按鈕網格佈局

<TableLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
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="fill_parent" > 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" /> 

    <Button 
     android:id="@+id/button2" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" /> 
</TableRow> 

<TableRow 
    android:id="@+id/tableRow2" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <Button 
     android:id="@+id/button3" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" /> 

    <Button 
     android:id="@+id/button4" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" /> 
</TableRow> 

回答

8

試試這個代碼。

<?xml version="1.0" encoding="utf-8"?> 
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/tableLayout1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

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

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

     <Button 
      android:id="@+id/button1" 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" /> 
    </TableRow> 

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

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

     <Button 
      android:id="@+id/button1" 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" /> 
    </TableRow> 

</TableLayout> 

你應該使用重量按鈕

+0

我是如此接近回答這個問題過於:( –

+0

感謝那乾淨 –

0

我希望這是什麼ü正在尋找:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/tableLayout1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:stretchColumns="*" > 

    <TableRow 
     android:id="@+id/tableRow1" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" > 

     <Button 
      android:id="@+id/button1" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" /> 

     <Button 
      android:id="@+id/button2" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" /> 
    </TableRow> 

    <TableRow 
     android:id="@+id/tableRow2" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" > 

     <Button 
      android:id="@+id/button3" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" /> 

     <Button 
      android:id="@+id/button4" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" /> 
    </TableRow> 

</TableLayout> 
7

我建議你用一個簡單的RelativeLayout沒有進入使用權的麻煩(使用LinearLayout,TableLayout或兩者的組合)。下面是一個示例佈局,展示瞭如何使用RelativeLayout實現覆蓋整個屏幕的2 X 2網格,從而使其更加高效和快捷。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

<View 
    android:id="@+id/centerVerticalShim" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_centerVertical="true" 
    android:visibility="invisible" /> 

<View 
    android:id="@+id/centerHorizontalShim" 
    android:layout_width="0dp" 
    android:layout_height="match_parent" 
    android:layout_centerHorizontal="true" 
    android:visibility="invisible" /> 

<TextView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_above="@+id/centerVerticalShim" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:layout_toLeftOf="@+id/centerHorizontalShim" 
    android:background="#42A5F5" 
    android:gravity="center" 
    android:text="@string/one" 
    android:textColor="#FFFFFF" > 
</TextView> 

<TextView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_above="@+id/centerVerticalShim" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentTop="true" 
    android:layout_toRightOf="@+id/centerHorizontalShim" 
    android:background="#EF5350" 
    android:gravity="center" 
    android:text="@string/two" 
    android:textColor="#FFFFFF" > 
</TextView> 

<TextView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true" 
    android:layout_below="@+id/centerVerticalShim" 
    android:layout_toLeftOf="@+id/centerHorizontalShim" 
    android:background="#66BB6A" 
    android:gravity="center" 
    android:text="@string/three" 
    android:textColor="#FFFFFF" > 
</TextView> 

<TextView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentRight="true" 
    android:layout_below="@+id/centerVerticalShim" 
    android:layout_toRightOf="@+id/centerHorizontalShim" 
    android:background="#5C6BC0" 
    android:gravity="center" 
    android:text="@string/four" 
    android:textColor="#FFFFFF" > 
</TextView></RelativeLayout> 

上述佈局的結果是:

+0

這個答案是非常好,更快和有效的應該是公認的答案! 。 –