2011-04-04 109 views
21

我試圖創建一個電話撥號器視圖,使用TableLayout創建一個3x4的網格中的12個按鍵。我希望這些行垂直拉伸以平均使用所有可用空間,但似乎fill_parent不適用於TableRows。的Android的TableRow垂直拉伸填滿屏幕

我想不會有使用setMinimumHeight - 是有沒有辦法在佈局正確做到這一點?

非常感謝,

埃德

回答

57
<?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" 
> 
<TableLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:stretchColumns="*" 
    > 
    <TableRow 
     android:layout_weight="1" 
     android:gravity="center"> 
     <Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/> 
     <Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/> 
     <Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/> 
    </TableRow> 
    <TableRow 
     android:layout_weight="1" 
     android:gravity="center"> 
     <Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/> 
     <Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/> 
     <Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/> 
    </TableRow> 
    <TableRow 
     android:layout_weight="1" 
     android:gravity="center"> 
     <Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/> 
     <Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/> 
     <Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/> 
    </TableRow> 
    <TableRow 
     android:layout_weight="1" 
     android:gravity="center"> 
     <Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/> 
     <Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/> 
     <Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/> 
    </TableRow> 
</TableLayout> 
</LinearLayout> 

enter image description here

+0

我要張貼在2個嵌套Linearlayouts與按鈕的解決方案,但似乎你的解決方案正常工作過,也是使用TableLayout,如OP所要求的。 :) – Adinia 2011-04-04 13:30:52

+0

很酷,但如何在java代碼中做到這一點?我試圖讓只在運行時已知的行數和列數類似的結果,但得到'java.lang.ArithmeticException:在'android.widget.TableLayout.mutateColumnsWidth zero'(TableLayout.java:579)'鴻溝。甚至不能相信Android佈局系統真的非常遲鈍。 – 2013-03-23 20:24:40

+1

哎呀,我發現它:http://stackoverflow.com/a/9683743/1418097 – 2013-03-23 20:44:55