在Android上列出布局,如何使按钮右对齐?

时间:2011-10-22 16:45:35

标签: android

我一直在寻找使按钮右对齐的方法,但没有成功。这里需要一些帮助。

<LinearLayout android:id="@+id/categoryRowLayout"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <TextView android:text="category" android:id="@+id/categoryRow"
        android:layout_width="wrap_content" android:layout_height="wrap_content">
    </TextView>
    <Button android:text="Edit" android:id="@+id/editCategoryBtn"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:onClick="editCategoryHandler">
    </Button>
    <Button android:text="Delete" android:id="@+id/deleteCategoryBtn"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:onClick="deleteCategoryHandler">
    </Button>

</LinearLayout>

截图:

enter image description here

2 个答案:

答案 0 :(得分:2)

    <RelativeLayout android:id="@+id/categoryRowLayout"
        android:layout_width="fill_parent" android:layout_height="fill_parent"
        xmlns:android="http://schemas.android.com/apk/res/android">

        <TextView android:text="category" android:id="@+id/categoryRow"
            android:layout_width="wrap_content" android:layout_height="wrap_content">
        </TextView>
        <Button android:text="Edit" android:id="@+id/editCategoryBtn"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:onClick="editCategoryHandler"
            android:layout_toLeftOf="@+id/deleteCategoryBtn"
    >
        </Button>
        <Button android:text="Delete" android:id="@+id/deleteCategoryBtn"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:onClick="deleteCategoryHandler"
            android:layout_alignParentRight="true"
    >
        </Button>

</RelativeLayout>

试试这个,我使用了相对布局和右边的删除按钮,然后编辑删除按钮左侧的按钮。

答案 1 :(得分:0)

作为替代方案,您可以刚刚添加到TextView:

android:layout_weight="1"