Android TableRow垂直拉伸以填充屏幕

时间:2011-04-04 12:22:00

标签: android

我正在尝试创建一个电话拨号器视图,使用TableLayout在3x4网格中创建12个按钮。我希望行垂直拉伸以平等地消耗所有可用空间,但看起来fill_parent在TableRows上不起作用。

我不想使用setMinimumHeight - 有没有办法在布局中正确地做到这一点?

非常感谢,

1 个答案:

答案 0 :(得分:59)

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