Android 按钮不希望在TableLayout中向右移动

Android 按钮不希望在TableLayout中向右移动,android,tablelayout,tablerow,gravity,Android,Tablelayout,Tablerow,Gravity,这个代码有什么问题?为什么这一行的按钮不在右边 <TableLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TableRow> <TextView

这个代码有什么问题?为什么这一行的按钮不在右边

<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>

android:gravity=“right”
用于
按钮内的文本。因为它的宽度被设置为
wrap\u content
,所以根本没有效果

您需要改用
android:layou\u gravity
。见:

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

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

(……)

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


您应该在TableLayout中使用android:stretchColumns=“1”,这样您的EditText就可以将按钮向右推

或者您可以在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>


它粘在编辑文本(右侧)上。问题是我尝试了两种重力姿势,但都没有结果。我认为下面的帖子是对的:我忘记了属性
stretchColumns
。这是罗马尼亚人说的(可能会对某人有所帮助):
重力意味着“将重力应用于此视图的内容”,而layout_重力意味着“将重力应用于其父视图内的此视图”。因此,对于TextView,gravity将在文本视图的边界内对齐文本,而layout_gravity将在其父对象的边界内对齐文本视图。
android:stretchColumns=“1”
缺失。谢谢