2012-02-27 84 views
0

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

我试着把一些权重放到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,将所有可能的空间显示为网格? 这将真正解决我的问题,每个屏幕尺寸。

回答

0

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

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

+0

行动!我认为这还不是解决方案,因为我注意到在不同的屏幕尺寸下,即使使用重量,它也可能会再次发生。 有没有办法告诉TextView采取所有的垂直空间呢?也许你可以使用weightSum来做到这一点,但我做不到。 – Bakuriu 2012-02-27 19:52:36

+0

from开发者网站re:权重“例如,如果有三个文本框,其中两个声明权重为1,而另一个没有权重(0),则第三个没有权重的文本框不会增长,并且会只占用其内容所需的面积,另外两个将平均扩大,以便在测量完所有三个盒子后填满剩余空间。“因此,给textrow的权重为1的文本框,并将其他默认值设置为0,textview行应该展开以填充可用空间。 [more here](http://developer.android.com/guide/topics/ui/layout-objects.html) – Barak 2012-02-28 15:40:26