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

时间:2015-04-13 21:03:39

标签: android android-layout

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

Misaligned Buttons

这是布局的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>

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

3 个答案:

答案 0 :(得分:1)

你可以用两个垂直的线性布局来分隔空间, 在左边一个放你的textViews,在右边一个放你的按钮

答案 1 :(得分:1)

而不是使用&#34; wrap_content&#34;对于宽度,请为textviews使用一些固定值。或者,更好的是,用RelativeLayout替换整个事物,并使用android:layout_alignLeft指令将Button 5对齐到Button 1的左侧。

答案 2 :(得分:1)

尝试TableLayout:

Snip

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