android - 如何制作 4 个尺寸相同的 2x2 按钮

标签 android button

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

A B

CD

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

我尝试将以下设置与嵌套的 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 个 TableRowsLinearLayout 并在每个 TableRow 中插入 2 个 Buttons。 根据您的需要分配 widthheight。 为包括 TableRows 在内的每个项目分配 1 的 weight,您应该可以开始了。

编辑:好的,玩了一会儿后我发现这很好用 ;-)

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

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

关于android - 如何制作 4 个尺寸相同的 2x2 按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15932884/

相关文章:

java - 安卓 : SMSManager - Attempt to get length of null array

android - 如何处理wifi和移动数据之间的网络变化?

android - 在设备上的模拟环境中启动 Android 内核

android - 通用(平板电脑和手机)应用程序 - 如何最好地构建 - 多布局 xml 或显示/隐藏小部件?

android - Appcompat按钮背景样式

android - jetpack compose 中的滑动按钮菜单

android - 动态添加 TableRow 与 getMeasuredHeight/getHeight 的问题

android - 增加按钮的可点击区域

android - ViewGroup 中的 ImageButton

java - 模拟器无法播放声音