有 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_weight
的 C1
、C2
和 C3
设置为 {{1} } 0.25
和 0.25
我得到了想要的对齐。
问题:我做错了什么,因为我最初让每个 0.5
拥有属性 TextView
的解决方案没有按预期工作?
答案 0 :(得分:2)
你没有做错任何事,你得到的结果是意料之中的。
您只需要将 android:weightSum
加到 1 到第三个 LinearLayout。因为,默认情况下,android:weightSum
的值设置为孩子的 layout_weight
答案 1 :(得分:0)
试试这个...
<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>