Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

android:layout_weight 深入解析:五組對照實驗看懂分配規則

編輯:關於 Android 程式設計

layout_weight 是 LinearLayout 最常用的屬性之一,但它的行為並不像表面那樣直白——當子元素的 layout_width 不是 0dp 時,分配結果往往出乎意料。本文用五組對照實驗,把內部的計算規則完整推一遍。

實驗一:最直觀的用法

圖 1

三個文本框的寬度比為 1 : 2 : 3

圖 2

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/text_view1"
        android:layout_width="0dp"
        android:layout_height="48dp"
        android:layout_weight="1"
        android:background="#44ff0000"
        android:gravity="center"
        android:text="1" />

    <TextView
        android:id="@+id/text_view2"
        android:layout_width="0dp"
        android:layout_height="48dp"
        android:layout_weight="2"
        android:background="#4400ff00"
        android:gravity="center"
        android:text="2" />

    <TextView
        android:id="@+id/text_view3"
        android:layout_width="0dp"
        android:layout_height="48dp"
        android:layout_weight="3"
        android:background="#440000ff"
        android:gravity="center"
        android:text="3" />

</LinearLayout>

三個 TextView 的 layout_width 都設為 0dp,layout_weight 為 1:2:3,因此佔寬比也是 1:2:3。這是想用 weight 按比例分欄時的標準寫法。


實驗二:baselineAligned 的隱形影響

圖 3

圖 4

對比可發現:圖 3 中第一個文本框與第二、三個不對齊;圖 4 中三者完全對齊。

圖 3 的佈局代碼

圖 5

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/text_view1"
        android:layout_width="0dp"
        android:layout_height="48dp"
        android:layout_weight="1"
        android:background="#44ff0000"
        android:gravity="center"
        android:text="111111111111111111" />
    ...
</LinearLayout>

圖 4 的佈局代碼

圖 6

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:baselineAligned="false">

    <TextView
        android:id="@+id/text_view1"
        android:layout_width="0dp"
        android:layout_height="48dp"
        android:layout_weight="1"
        android:background="#44ff0000"
        android:gravity="center"
        android:text="111111111111111111" />
    ...
</LinearLayout>

兩者唯一的差別,是父容器多設了 android:baselineAligned="false"。

原理:LinearLayout 預設會以子元素的文字 baseline 作為對齊基準,當某個子元素的文字較長時,它就會被視為「基準項」,導致其他子元素整體下移錯位。做純粹的比例分欄時,加上這個屬性關閉 baseline 對齊即可。


實驗三:wrap_content 與 0dp 的視覺錯覺

圖 7

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:baselineAligned="false">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="48dp"
        android:layout_weight="1"
        android:text="111111111111111111" />
    <TextView
        android:layout_width="0dp"
        android:layout_height="48dp"
        android:layout_weight="2" />
    <TextView
        android:layout_width="0dp"
        android:layout_height="48dp"
        android:layout_weight="3" />
</LinearLayout>

圖 8

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:baselineAligned="false">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="48dp"
        android:layout_weight="1"
        android:text="111111111111111111" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="48dp"
        android:layout_weight="2" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="48dp"
        android:layout_weight="3" />
</LinearLayout>

圖 7 與圖 8 看起來幾乎一樣。
· 圖 7:textView1 為 wrap_content,textView2/3 為 0dp。
· 圖 8:三者皆為 wrap_content。
在這個特定內容下視覺一致,但一旦文字長度變化,兩者的分配結果就會分岔——所以不能靠「看起來一樣」來推廣結論。


實驗四:match_parent 下的反直覺結果

圖 9

三個 TextView 的 layout_width 全部設為 match_parent,weight 為 1:2:3。

圖 10

同樣的佈局,只把 textView3 的 weight 從 3 改成 2。

圖 9 中 TextView3 完全沒有顯示出來,而圖 10 中三者都正常顯示。
僅僅一個 weight 數值的差異,就讓一個控件徹底消失。

圖 11

要理解這個現象,得先記住 LinearLayout 的兩條核心規則:

規則一:佈局首先按控件自己聲明的尺寸進行分配,剩下的尺寸再按 weight 分配。
規則二:控件的最終寬度 = 聲明的寬度 + 父容器剩餘寬度 × 所佔比例。

假設父容器寬 480dp,逐一套入計算:

圖 9 的計算(weight 為 1:2:3,總和 6)

第一步,剩餘尺寸:三個 TextView 都声明 480dp,故

restWidth = 480 - 480 × 3 = -480 × 2

第二步,各自最終寬度:

width1 = 480 + (-480 × 2) × (1/6) = 480 × (4/6)
width2 = 480 + (-480 × 2) × (2/6) = 480 × (2/6)
width3 = 480 + (-480 × 2) × (3/6) = 480 × (0/6)

所以 width1 : width2 : width3 = 2 : 1 : 0——第三個 view 寬度為 0,自然看不見。

圖 10 的計算(weight 為 1:2:2,總和 5)

restWidth = 480 - 480 × 3 = -480 × 2

width1 = 480 + (-480 × 2) × (1/5) = 480 × (3/5)
width2 = 480 + (-480 × 2) × (2/5) = 480 × (1/5)
width3 = 480 + (-480 × 2) × (2/5) = 480 × (1/5)

比值為 3 : 1 : 1,三者都有實際寬度,因此都顯示了出來。

這也解釋了為什麼用 weight 做比例分欄時,width 必須設為 0dp:只有讓「聲明寬度」歸零,剩餘寬度才等於父容器全寬,比例才是你寫的那個比例。


實驗五:weightSum 的作用

圖 12

當需要讓子控件精確佔據父寬的 1/2、1/3 時,就要在父容器上設定 android:weightSum。

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:weightSum="3">

    <TextView
        android:layout_width="0dp"
        android:layout_height="48dp"
        android:layout_weight="1"
        android:text="111111111111111111" />

</LinearLayout>

若不設 weightSum:

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <TextView
        android:layout_width="0dp"
        android:layout_height="48dp"
        android:layout_weight="1"
        android:text="111111111111111111" />

</LinearLayout>

計算過程(父寬 480dp):

第一步,剩餘尺寸——此時聲明寬度為 0dp:

restWidth = 480 - 0 = 480

第二步,套用規則二(這裡比例要除以 weightSum):

width = 0 + 480 × (1 / 3) = 160

即恰好佔父容器的 1/3。

圖 13

由此可見 weightSum 的意義:它顯式聲明權重總和,讓「weight=1 佔三分之一」這類語意得以成立。不設時,系統會把所有子元素 weight 加總作為分母;設了,則以你指定的值為分母。當子元素 weight 總和不等於 weightSum 時,剩下的空間會被留白,這在刻意排版時反而很好用。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved