Android:指定TableRows的大小

时间:2012-02-27 18:24:39

标签: java android layout size

我一直在尝试创建一个简单的布局。它是一个3x3网格,下面是TextView。 我使用TableLayout来设置ImageButtons和TextView(TextView具有layout_span = 3)。 问题是在横向模式下我看不到TextView。

我试图给TableRows增加一些权重,但这解决了横向模式的问题,造成了另一个问题:在纵向模式下,按钮之间有很大的空间。

我正在尝试创建的是一个刻度线脚趾游戏,所以我希望按钮是紧凑的。

我怎样才能获得这样的效果?我希望Textview能够占用所有剩余的垂直空间,我希望它在横向模式下也可见。

对此有何解决方案?

这是xml代码:

<?xml version="1.0" encoding="utf-8"?>

<!--suppress AndroidDomInspection -->
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:background="#000000"
              android:stretchColumns="*">
    <TableRow android:layout_margin="2dip"
              android:background="#000000">
        <ImageButton android:id="@+id/z_z" android:layout_margin="3dip"
                     android:layout_height="wrap_content"
                     android:layout_width="wrap_content"
                     android:src="@drawable/empty"
                     android:background="#ffffff"/>
        <ImageButton android:id="@+id/z_o" android:layout_margin="3dip"
                     android:layout_height="wrap_content"
                     android:layout_width="wrap_content"
                     android:src="@drawable/empty"
                     android:background="#ffffff"/>
        <ImageButton android:id="@+id/z_t" android:layout_margin="3dip"
                     android:layout_height="wrap_content"
                     android:layout_width="wrap_content"
                     android:src="@drawable/empty"
                     android:background="#ffffff"/>
    </TableRow>
    <TableRow android:layout_margin="2dip"
              android:background="#000000">
        <ImageButton android:id="@+id/o_z" android:layout_margin="3dip"
                     android:layout_height="wrap_content"
                     android:layout_width="wrap_content"
                     android:src="@drawable/empty"
                     android:background="#ffffff"/>
        <ImageButton android:id="@+id/o_o" android:layout_margin="3dip"
                     android:layout_height="wrap_content"
                     android:layout_width="wrap_content"
                     android:src="@drawable/empty"
                     android:background="#ffffff"/>
        <ImageButton android:id="@+id/o_t" android:layout_margin="3dip"
                     android:layout_height="wrap_content"
                     android:layout_width="wrap_content"
                     android:src="@drawable/empty"
                     android:background="#ffffff"/>
    </TableRow>
    <TableRow android:layout_margin="2dip"
              android:background="#000000">
        <ImageButton android:id="@+id/t_z" android:layout_margin="3dip"
                     android:layout_height="wrap_content"
                     android:layout_width="wrap_content"
                     android:src="@drawable/empty"
                     android:background="#ffffff"/>
        <ImageButton android:id="@+id/t_o" android:layout_margin="3dip"
                     android:layout_height="wrap_content"
                     android:layout_width="wrap_content"
                     android:src="@drawable/empty"
                     android:background="#ffffff"/>
        <ImageButton android:id="@+id/t_t" android:layout_margin="3dip"
                     android:layout_height="wrap_content"
                     android:layout_width="wrap_content"
                     android:src="@drawable/empty"
                     android:background="#ffffff"/>
    </TableRow>
    <TableRow>
        <TextView android:id="@+id/message_txt_view"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="@string/how_to_play"
                  android:textSize="@dimen/message_size"
                  android:layout_span="3"/>

    </TableRow>

</TableLayout>

编辑: 我已经了解了如何使用权重,但我认为这实际上并没有解决我的问题,因为在不同的屏幕尺寸上,网格仍然是格式错误的。 我真正想要做的是在布局代码中使用屏幕大小的方法。 通过这种方式,我可以使用适合它的权重。

可能我可以为每个屏幕维度指定不同的布局(我在一两天前读过这样的内容),但这会有点无聊。如果我改变布局,我还需要更新大量的文件。

也许有一种方法可以将TableLayout高度设置为wrap_content,同时将TextView高度设置为fill_parent,占用所有空间以显示网格? 这将真正解决我的每个屏幕尺寸的问题。

1 个答案:

答案 0 :(得分:0)

使用您提到的权重为横向模式创建不同的布局,并保持纵向模式不变。

要使用它,只需将两个布局命名为相同。将肖像放在通常的“布局”目录中,并将横向放置在新目录“layout-land”中。