如何在 Android 中对齐这些按钮?

How do I align these buttons in Android?

我希望这些按钮彼此对齐。如您所见,由于第二行中的文本较短,因此按钮占用了更多 space 来填满所有内容。我宁愿将按钮 5 直接放在下方并与按钮 1 对齐。

这是我的 xml 布局:

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingBottom="10dp">
        <TextView android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:gravity="center_vertical"
            android:text="Start Corner:"
            />

        <Button
            android:id="@+id/start_corner_btn_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="1"
            android:layout_weight="15"/>

        <Button
            android:id="@+id/start_corner_btn_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="2"
            android:layout_weight="15"/>

        <Button
            android:id="@+id/start_corner_btn_3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="3"
            android:layout_weight="15"/>

        <Button
            android:id="@+id/start_corner_btn_4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="4"
            android:layout_weight="15"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingBottom="10dp">
        <TextView android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:gravity="center_vertical"
            android:text="2nd Corner:"
            />

        <Button
            android:id="@+id/second_corner_btn_5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="5"
            android:layout_weight="15"/>

        <Button
            android:id="@+id/second_corner_btn_6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="6"
            android:layout_weight="15"/>

        <Button
            android:id="@+id/second_corner_btn_7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="7"
            android:layout_weight="15"/>

        <Button
            android:id="@+id/second_corner_btn_8"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="8"
            android:layout_weight="15"/>
    </LinearLayout>

一如既往,我敢打赌我很接近,但可能遗漏了一件事!

你可以用两个垂直的线性布局来分隔 space, 在左边放你的 textViews 在右边放你的 Buttons

不使用 "wrap_content" 作为宽度,而是为文本视图使用一些固定值。或者,更好的是,用 RelativeLayout 替换整个东西,并使用 android:layout_alignLeft 指令将 Button 5 对齐到 Button 1 的左侧。

尝试表格布局:

<TableLayout
    android:id="@+id/tableBtns"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dip" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:layout_weight="15"
            android:text="Start Corner:"/>

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="15"
            android:text="1" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="15"
            android:text="2" />

        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="15"
            android:text="3" />

        <Button
            android:id="@+id/button4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="15"
            android:text="4" />
    </TableRow>

    <TableRow
        android:id="@+id/tableRow2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dip" >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:layout_weight="15"
            android:text="2nd Corner:" />

        <Button
            android:id="@+id/button5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="15"
            android:text="5" />

        <Button
            android:id="@+id/button6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="15"
            android:text="6" />

        <Button
            android:id="@+id/button7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="15"
            android:text="7" />

        <Button
            android:id="@+id/button8"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="15"
            android:text="8" />
    </TableRow>
</TableLayout>