【问题标题】:Nested LinearLayout widget design with different weights具有不同权重的嵌套 LinearLayout 小部件设计
【发布时间】:2015-04-22 19:52:58
【问题描述】:

多年来,我一直在尝试使以下布局适用于小部件,并且对布局缺乏经验,这让我有些悲伤,起初我有相对布局,但它似乎没有根据手机尺寸和文字输入。

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

盒子 8 和 9 的高度各为 7 的一半,因此重量方面(据我所知)将是 7 的每一个。7 是长度的一半,而 8 + 9 是另一半(长度方面)。

10 和 11 是宽度的一半。

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

【问题讨论】:

  • 您可以轻松地使用线性布局的权重属性来实现它,而不是使用网格布局。首先您必须清除您的设计模式。
  • GridLayout 应该是最好的选择。
  • 我会试试 GridLayout 和 LinerLayout 看看有什么不同。
  • 使用线性布局并使用权重属性。

标签: android android-layout android-linearlayout


【解决方案1】:

查看以下代码:

<?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,布局部分会相应地扩大。

【讨论】:

  • 我只能在 5 小时内检查这个。但是,是的,这就是我想要实现的目标。
【解决方案2】:

您的问题非常广泛,但我会将 root 设为 RelativeLayout 并在 LinearLayout 中分组元素(数字 2、3、4 是一种布局,5,6 - 秒等)。因此,每个LinearLayout 都可以使用layout_belowlayout_parentBottom 等属性轻松管理。 查看RelativeLayout here 的孩子的所有属性。 我希望这会有所帮助。

【讨论】:

  • 是的,这对我有点帮助。对广泛的问题道歉。如果这样做,我只想看看不同的方式。它将帮助我理解布局。
  • @Dino,没关系,只需选择最适合您的解决方案,因为您知道,有很多方法可以做到这一点,很难说 100% 什么是最好或最有效的方法这样做。
  • 是的,Yurets 是对的。使用RelativeLayouts 和Linearlayouts 来实现上述布局。网格布局会很困难
猜你喜欢
  • 1970-01-01
  • 2012-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-29
  • 1970-01-01
  • 2015-06-04
  • 2020-01-11
相关资源
最近更新 更多