2011-04-04 38 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