Button does not want to go right in TableLayout
What is wrong with this code? Why doesn't the button go right in this table row?
<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>
You should either use android:stretchColumns="1" in your TableLayout, so that your EditText pushes your Button to the right.
Or you can use android:layout_weight attribute on the views in a tableRow to distribute the space accordingly.
Also, you dont need to specify width and height attributes on the views in a 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>
android:gravity="right" is used for the text inside the Button. Since it's width is set to wrap_content, it has no effect at all.
You need to use android:layout_gravity instead. See: http://developer.android.com/reference/android/widget/LinearLayout.LayoutParams.html#attr_android:layout_gravity
Standard gravity constant that a child can supply to its parent. Defines how to place the view, both its x- and y-axis, within its parent view group.
Must be one or more (separated by '|') of the following constant values.
(...)
right 0x05 Push object to the right of its container, not changing its size.
 加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论