拉伸水平填充行xml

时间:2012-04-26 02:31:19

标签: android xml android-linearlayout tablerow

我可能错过了一些明显的东西,但我似乎无法解决这个问题。我正在寻找一个像1,5或6的结果,一行3个均匀分布的按钮,填充屏幕的宽度。然而不幸的是,这些方法中的每一种似乎都有问题。 1给我一个警告,“这个TableRow布局或它的TableLayout父可能是无用的”,5和6给出“嵌套权重对性能不利”。所以基本上我想知道最好的方法是什么。我应该忽略no.1上的警告,还是可以通过TableRow或TableLayout正确显示它们

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" >

<!-- No.1 -->
<TableLayout android:id="@+id/tableLayout1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:stretchColumns="*" >
    <TableRow android:id="@+id/tbRow0" android:layout_width="wrap_content" android:layout_height="wrap_content" >
        <Button android:id="@+id/btn1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/num1" />
        <Button android:id="@+id/btn2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/num2" />
        <Button android:id="@+id/btn3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/num3" />
    </TableRow>
</TableLayout>

<!-- No.2 -->
<TableRow android:id="@+id/tbRow1" android:layout_width="fill_parent" android:layout_height="wrap_content" >
    <Button android:id="@+id/btn4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/num1" />
    <Button android:id="@+id/btn5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/num2" />
    <Button android:id="@+id/btn6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/num3" />
</TableRow>

<!-- No.3 -->
<TableRow android:id="@+id/tbRow2" android:layout_width="fill_parent" android:layout_height="wrap_content" >
    <Button android:id="@+id/btn7" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/num1" />
    <Button android:id="@+id/btn8" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/num2" />
    <Button android:id="@+id/btn9" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/num3" />
</TableRow>

<!-- No.4 -->
<TableRow android:id="@+id/tbRow3" android:layout_width="fill_parent" android:layout_height="wrap_content" android:stretchColumns="*" >
    <Button android:id="@+id/btn10" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/num1" />
    <Button android:id="@+id/btn11" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/num2" />
    <Button android:id="@+id/btn12" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/num3" />
</TableRow>

<!-- No.5 -->
<TableRow android:id="@+id/tbRow4" android:layout_width="fill_parent" android:layout_height="wrap_content" >
    <Button android:id="@+id/btn13" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/num1" />
    <Button android:id="@+id/btn14" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/num2" />
    <Button android:id="@+id/btn15" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/num3" />
</TableRow>

<!-- No.6 -->
<LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" >
    <Button android:id="@+id/btn16" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/num1" />
    <Button android:id="@+id/btn17" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/num2" />
    <Button android:id="@+id/btn18" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/num3" />
</LinearLayout>

<!-- No.7 -->
<LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" >
    <Button android:id="@+id/btn19" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/num1" />
    <Button android:id="@+id/btn20" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/num2" />
    <Button android:id="@+id/btn21" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/num3" />
</LinearLayout>

</LinearLayout>

2 个答案:

答案 0 :(得分:1)

我认为5是正确的,加上我没有得到嵌套权重警告。

也许这与Agarwal指出的相关。

编辑:抱歉,我虽然使用的是LinearLayout。我的意思是:

<LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" >
    <Button android:id="@+id/btn13" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/num1" />
    <Button android:id="@+id/btn14" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/num2" />
    <Button android:id="@+id/btn15" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/num3" />
</LinearLayout>

答案 1 :(得分:0)

在第一行之后的xml文件中你正在关闭tablelayout,但即使你在2,5行中使用表行也可能是个问题。

如果你只需要一个,那么最好有线性布局,因为性能并不差,因为你有一个小的xml文件,这是好的。

相关问题