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