具有不同权重的嵌套 LinearLayout 小部件设计

Nested LinearLayout widget design with different weights

多年来我一直在尝试让以下布局适用于小部件,但我对布局缺乏经验,这让我有些伤心,起初我有相对布局,但它似乎没有自行调整大小根据 phone 大小和文本输入。

如下图所示,就是我想要的布局。框 1 到 11。我假设 LinearLayout 是最好的布局?所有框都将包含文本。 Bozes 2, 3, 4 是 box 1 高度的一半,box 5 和 6 也是 1 的一半(抱歉,这是一个有点垃圾的快速设计)。

方框 8 和 9 各占 7 个高度的一半,因此重量方面(据我所知)将是 7 个中各 1 个。7 是长度的一半,8 + 9 是另一半(长度方面) .

10和11分别是宽度的一半。

你认为 GridLayout 会更好吗考虑到框的宽度可以扩展或收缩几个数字,例如,如果有人选择华氏度而不是摄氏度,100 华氏度将有一个与 37 摄氏度相比的额外数字,或 32 华氏度将是 0 摄氏度。或者 200 毫米的雨水到英寸将是 8 英寸(缩小 2 位数)。

你的问题很宽泛,但我会将根设为 RelativeLayout 并在 LinearLayout 中对元素进行分组(数字 2、3、4 是一种布局,5、6 - 第二种,依此类推).因此,每个 LinearLayout 您都可以使用 layout_belowlayout_parentBottom 等属性轻松管理。 查看 RelativeLayout here 的 children 的所有属性。 希望对您有所帮助。

检查下面的代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@color/background_floating_material_light">

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

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:text="1" />
        </LinearLayout>

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

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="#e3e2ad">

                <TextView
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:text="2" />

                <TextView
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:text="3"
                    android:background="#aacaff" />

                <TextView
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:text="4" />

            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1">

                <TextView
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:text="5"
                    android:background="#bcf5b1" />

                <TextView
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:text="6" />
            </LinearLayout>

        </LinearLayout>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical"
            android:background="@color/dim_foreground_disabled_material_dark">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:text="7" />
        </LinearLayout>

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

            <TextView
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:gravity="center"
                android:text="8"
                android:background="#e3e2ad" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:gravity="center"
                android:text="9" />

        </LinearLayout>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@color/primary_material_light">

        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="10"
            android:background="#bcf5b1" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="11" />
    </LinearLayout>

</LinearLayout>

输出:

我用 Textview 来显示数字。你可以用任何东西代替。 如果您有任何疑问,请询问。此外,如果您在正确的位置使用 wrap_content,布局部分将相应扩展。