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

深入剖析 layout_weight:為什麼權重越大反而越小

編輯:關於 Android 程式設計

前言

對常跟 UI 打交道的開發者來說,layout_weight 是再熟悉不過的屬性。但真正用它時,常常會出現出乎意料之外的版面結果。如果只記口訣而不懂原理,這些「意外」遲早會讓你熬夜。本文就把它徹底講清楚。

從最簡單的例子開始

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

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="button1" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="button2" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="button3" />
</LinearLayout>

這三顆按鈕並不會佔滿整個螢幕,而是各自依內容高度呈現。

接著只幫 button2 加上 android:layout_weight="1",版面整個變了:三顆按鈕撐滿螢幕,而 button1 與 button3 仍維持內容高度,多出來的空間全給了 button2。

原理拆解

  1. button1 與 button3 沒有設定 weight,數值為 0。
  2. 當 LinearLayout 偵測到子 View 帶有 layout_weight 時,會進行兩次 measure:第一次正常計算各子 View 的寬高,第二次再把剩餘空間依權重分配。

白話來說:系統先依 wrap_content 把三顆按鈕的高度定下來,然後把剩下的螢幕空間全給權重不為 0 的 button2。這就是它看起來特別「胖」的原因。

三個 TextView 的權重分配

<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="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="1"
        android:background="#ff0000" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:text="2"
        android:background="#00ff00" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:text="3"
        android:background="#0000ff" />
</LinearLayout>

結果完全符合上面的推論:系統先給三個 TextView 足以容納「1」「2」「3」的寬度,再把剩餘空間依 1:2:3 的比例分出去。

反直覺的情況:權重越大反而越小

如果把三個 TextView 的 layout_width 都改成 match_parent,權重設為 1、2、2,你會看到寬度比例變成 3:1:1——權重最大的那個反而最窄。

網路上流傳的說法是:match_parent 時 weight 代表「優先盡可能大」,wrap_content 時代表「優先盡可能小」。口訣好背,但忘記時就麻煩了,我們直接算一次。

  1. 第一次 measure:三個 TextView 都被指定為 match_parent,也就是每個都佔滿整個螢幕寬度(記為 parent_width)。
  2. 第二次 measure:剩餘空間 = 1 × parent_width − 3 × parent_width = −2 parent_width,是負的。
  3. 第一個 TextView 實際寬度 = parent_width + (1/5) × (−2 parent_width) = 3/5 parent_width
  4. 第二個 = parent_width + (2/5) × (−2 parent_width) = 1/5 parent_width
  5. 第三個同樣 = 1/5 parent_width

3/5 : 1/5 : 1/5 就是 3:1:1,完全對得上。

那如果權重改成 1、2、3 呢?第三個 TextView 會直接消失。同樣算一次:總權重 6,剩餘空間仍是 −2 parent_width。

  • 第一個 = parent_width + (1/6)(−2 parent_width) = 2/3 parent_width
  • 第二個 = parent_width + (2/6)(−2 parent_width) = 1/3 parent_width
  • 第三個 = parent_width + (3/6)(−2 parent_width) = 0

比例是 2:1:0,第三個連一點空間都分不到,自然看不見了。

懂了這個計算方式,之後遇到 match_parent 與 wrap_content 混用的複雜版面,都能自己算出結果,不必再靠試誤。

順帶談 weightSum

weightSum 用來明確指定權重總和;不指定的話,系統會把所有子 View 的 layout_weight 加總作為分母。

例如要做一个永遠佔半個螢幕的按鈕:

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

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="button" />
</LinearLayout>

官方文件對 weightSum 的描述是:定義權重總和的最大值;若未指定,則以所有子 View 的 layout_weight 累加值作為總和。文件舉的典型例子是:把子 View 的 layout_weight 設為 0.5、LinearLayout 的 weightSum 設為 1.0,即可讓子 View 佔據 50% 的可用寬度。

這裡我們把 weightSum 設為 2,按鈕權重為 1(總和的一半),寬度設 0dp,就能讓它穩定佔一半螢幕,不必用任何硬編碼的像素值。順帶一提,在 LinearLayout 中使用權重時,該方向的尺寸寫 0dp 會比 wrap_content 更有效率,因為可以少做一次不必要的計算。

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