繁体   English   中英

线性布局在空白视图中的行为如何?

[英]How does Linear Layout behaves with empty views?

在线性布局中,嵌套的窗口小部件根据其宽度/高度水平/垂直排列。
我的问题如下:

<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,要么都不显示,要么都不显示。 但是我在设计预览中看到了: 在此处输入图片说明

另外,如果我在第一个View添加android:layout_weight="2" ,在第二个View android:layout_weight="1" ,则即使我在封闭布局中添加weight_sum = 3,第二个视图也会获得2/3的空间

您仅看到一个视图,因为您没有给视图指定数字宽度。 因此,第一个视图将显示所有屏幕,而根本看不到其他视图-它在右侧,红色视图旁边。

如果您希望它们的宽度相同,则给彼此相同的重量,这样它们将彼此相等并适合屏幕的高度或宽度,具体取决于容器的布局方向。

从文档中:

android:weightSum-定义最大重量总和。 如果未指定,则通过将所有子项的layout_weight相加来计算总和。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM