简体   繁体   中英

How does Linear Layout behaves with empty views?

In a linear layout the nested widgets are arranged horizontaly/vertically and according to their width/height.
My question is in the following:

<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>  

Why do I see only the first view? I was expecting since they were wrap_content either both would display or none. But I see in the design preview: 在此处输入图片说明

Also if I add android:layout_weight="2" in the first View and android:layout_weight="1" in the second the second view gets 2/3 of the space even if I add weight_sum=3 in the enclosing layout

You see only one view because you didn't give a numeric width to the views. Because of that the first view gets all the screen and other one is not seen at all - it is at the right, next to the red one.

if you want that both of them will be with same width, just give each one the same weight, that way they will be equal to each other and will fit to the screen height or width, depends on the layout orientation of the container.

From the documentation:

android:weightSum - Defines the maximum weight sum. If unspecified, the sum is computed by adding the layout_weight of all of the children.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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