在线性布局中,嵌套小部件按水平/垂直方向排列,并根据宽度/高度排列 我的问题如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<View
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ffff0000"
/>
<View
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ff00ff00"
/>
</LinearLayout>
</LinearLayout>
为什么我只看到第一个视图?我期待,因为他们是wrap_content要么显示,要么没有。但我在设计预览中看到:
此外,如果我在第一个android:layout_weight="2"
和View
中添加android:layout_weight="1"
,则第二个视图会获得2/3的空间,即使我在封闭布局中添加了weight_sum = 3
答案 0 :(得分:2)
您只能看到一个视图,因为您没有为视图提供数字宽度。 因此,第一个视图获取所有屏幕,而其他视图根本看不到 - 它位于右侧,旁边是红色屏幕。
如果你想要它们两个都具有相同的宽度,只要给每个相同的重量,那么它们将彼此相等并且将适合屏幕的高度或宽度,取决于它们的布局方向容器
来自文档:
android:weightSum - 定义最大权重总和。如果未指定,则总和通过计算 添加所有孩子的layout_weight。