困惑于TableLayout中的“fill_parent”

时间:2012-02-01 15:42:37

标签: android layout tablelayout

首先,xml如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TableLayout
        android:layout_width = "wrap_content"
        android:layout_height = "wrap_content"
        android:stretchColumns = "1">

        <TableRow>
            <TextView
                android:text = "Test Layout:"
                android:id = "@+id/LayoutTextView01"
                android:layout_width = "wrap_content"
                android:layout_height = "wrap_content"
                android:textColor = "@color/red_bg" />
            <EditText
                android:text = ""
                android:id = "@+id/EditText01"
                android:layout_width = "fill_parent"
                android:layout_height = "wrap_content" />
        </TableRow>

        <TableRow android:gravity = "right">
            <Button
                android:text = "test"
                android:id = "@+id/layoutButton01"
                android:layout_width = "fill_parent"
                android:layout_height = "wrap_content" />
        </TableRow>

    </TableLayout>


</LinearLayout>

我的问题是:

1. fill_parent表示填充其父容器的所有空格。 Button 的父级应该具有相同大小的 EditView 的父级,以及 < em>按钮 EditView 都将android:layout_width设置为“fill_parent”,它们应该具有相同的大小,就像那样:

________ ___________________
|_______| |__________________|
              __________________
             |__________________|

但结果是这样的:

________ ___________________
|_______| |__________________|
                         ___________
                         |__________|

为什么?

2 个答案:

答案 0 :(得分:0)

如果您向android:layout_column="1"添加button tag,也许会有所帮助。

顺便说一下。我如何推荐问题而不是提交答案?

答案 1 :(得分:0)

问题是你的按钮认为它在第0列。如果你添加另一个元素,它将在第1列并具有相同的宽度。我试了一下,这应该会给你你想要的结果。

<TableRow android:gravity = "right">
        <TextView
            android:text = ""
            android:layout_width = "wrap_content"
            android:layout_height = "wrap_content" />
        <Button
            android:text = "test"
            android:id = "@+id/layoutButton01"
            android:layout_width = "fill_parent"
            android:layout_height = "wrap_content" />
</TableRow>

快乐的编码。