0

这段代码有什么问题?为什么按钮不在此表行中?

<TableLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            >
        <TableRow>
            <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/str1"
                    />
            <EditText
                    android:id="@+id/id1"
                    android:layout_width="70dp"
                    android:layout_height="wrap_content"
                    />
            <Button
                    android:id="@+id/id2"
                    android:layout_width="wrap_content"
                    android:layout_height="65dp"
                    android:text="@string/str2"
                    android:gravity="right"/>
        </TableRow>
</TableLayout>
4

2 回答 2

4

您应该在 TableLayout 中使用android:stretchColumns="1",以便您的 EditText 将您的 Button 推到右侧。

或者您可以在 tableRow 中的视图上使用android:layout_weight属性来相应地分配空间。

此外,您不需要在 TableRow 中的视图上指定宽度和高度属性。

<TableLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:stretchColumns="1">
        <TableRow>
            <TextView
                android:text="@string/str1"
                />
            <EditText
                android:id="@+id/id1"
                 />
            <Button
                android:id="@+id/id2"
                android:text="@string/str2"
                />
        </TableRow>
    </TableLayout>
于 2011-06-07T18:26:52.027 回答
3

android:gravity="right"用于Button. 由于它的宽度设置为wrap_content,它根本没有效果。

你需要android:layout_gravity改用。请参阅:http: //developer.android.com/reference/android/widget/LinearLayout.LayoutParams.html#attr_android :layout_gravity

子代可以提供给其父代的标准重力常数。定义如何在其父视图组中放置视图,包括其 x 轴和 y 轴。

必须是以下常量值中的一个或多个(由“|”分隔)。

(...)

right 0x05 将对象推到其容器的右侧,不改变其大小。

于 2011-06-07T17:54:02.197 回答