如何使4个2x2按钮具有相同的尺寸

时间:2013-04-10 17:50:25

标签: android button

我想创建一组2by2按钮,彼此对齐,如下所示:

A B

C D

每个都具有相同的宽度和高度,无论其内容的长度如何。

我尝试使用嵌套LinearLayouts的以下设置,它符合我的要求,但是我收到一条警告,说嵌套的layout_weight对性能有很大的(指数级)影响。

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:baselineAligned="false">

    <LinearLayout android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="50"
        android:orientation="vertical">

        <Button
            android:id="@+id/buttonA"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="50"
            android:text="aaa"/>

        <Button
            android:id="@+id/buttonC"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="50"
            android:text="cccccccccccccccccccccccccccc"/>
    </LinearLayout>

    <LinearLayout android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="50"
        android:orientation="vertical">

        <Button
            android:id="@+id/buttonB"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="50"
            android:text="bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" />

        <Button
            android:id="@+id/buttonD"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="50"
            android:text="ddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"/>
    </LinearLayout>
    </LinearLayout>

我想知道是否有更好的方法来做到这一点? 谢谢!

2 个答案:

答案 0 :(得分:1)

LinearLayoutTableRows一起使用,并在每个Buttons中插入2 TableRow。 根据您的需要分配widthheight。 为每个项目(包括weight)分配TableRows为1,您应该很高兴。

编辑:好好玩了一下我发现这个效果很好; - )

这基本上是你自己的布局和我的混合; - )

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1" >

        <Button
            android:id="@+id/button1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="cccccccccccccccccccccccccccc" />

        <Button
            android:id="@+id/button2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="Button" />
    </TableRow>

    <TableRow
        android:id="@+id/tableRow2"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1" >

        <Button
            android:id="@+id/button3"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" />

        <Button
            android:id="@+id/button4"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="ddddddddddddddddddddddddddddddddddddddddddddddddddddddddd" />
    </TableRow>

</LinearLayout>

答案 1 :(得分:0)

我还没有测试过,但您可以使用button.getWidth()来获取4个按钮的宽度。找到4的最大值,然后将所有按钮设置为该宽度。如果按钮的内容是动态的。