将子项设置为父项的百分比宽度

问题描述

有 3 个 LinearLayout 容器,方向为 horizontal。前两个 LinearLayout 各有 4 个 TextView 孩子。第三个只有 3 个。我已经为所有 TextView 子项创建并分配了样式。样式片段:

<item name="android:layout_width">0dp</item>
<item name="android:layout_weight">.25</item>

在这种样式下,前两行很好地对齐,每个孩子 TextView 使用父母宽度的四分之一。但是,第三个 TextView 中的 LinearLayout 组件每个都使用父宽度的三分之一。

所以组件对齐是这样的:

A1xxxx A2xxxx A3xxxx A4xxxx
B1xxxx B2xxxx B3xxxx B4xxxx
  C1xxxx  C2xxxx  C3xxxx

而不是这样(正如预期的那样):

A1xxxx A2xxxx A3xxxx A4xxxx
B1xxxx B2xxxx B3xxxx B4xxxx
C1xxxx C2xxxx C3xxxx

解决方案: 我尝试手动更改第三行中每个孩子的 android:layout_weight 属性,如果我将 layout_weightC1C2C3 设置为 {{1} } 0.250.25 我得到了想要的对齐。

问题:我做错了什么,因为我最初让每个 0.5 拥有属性 TextView解决方案没有按预期工作?

解决方法

你没有做错任何事,你得到的结果是意料之中的。

您只需要将 android:weightSum 加到 1 到第三个 LinearLayout。因为,默认情况下,android:weightSum 的值设置为孩子的 layout_weight

,

试试这个...

 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="5"
                android:text="xhajxnuwxnu"/>
            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="5"
                android:text="xhajxnuwxnu"/>
            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="5"
                android:text="xhajxnuwxnu"/>
            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="5"
                android:text="xhajxnuwxnu"/>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="5"
                android:text="xhajxnuwxnu"/>
            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="5"
                android:text="xhajxnuwxnu"/>
            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="5"
                android:text="xhajxnuwxnu"/>
            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="5"
                android:text="xhajxnuwxnu"/>
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="5"
                android:text="xhajxnuwxnu"/>
            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="5"
                android:text="xhajxnuwxnu"/>
            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="5"
                android:text="xhajxnuwxnu"/>
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="5"
                />
        </LinearLayout>
    </LinearLayout>

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...