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

徹底搞懂 Android 的 layout_weight:剩餘空間到底是怎麼分的

編輯:關於 Android 程式設計

寫 Demo 時碰到 layout_weight,網上關於它的討論很多,但多數說法流於口訣——「值越小權重越大」這類結論背誦起來容易,遇到實際問題就完全用不上。我自己把資料看完並動手驗證後,發現只要把「剩餘空間」這四個字理解對,所有看似矛盾的現象都能用同一套算式解釋。

一、先搞懂什麼是剩餘空間

看這段配置:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="left"
        android:text="one" />

    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_weight="1.0"
        android:text="two" />

    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="right"
        android:text="three" />

</LinearLayout>

執行結果:

只有第二個控制項設了 layout_weight="1.0",其餘兩個沒設,依 API 文件預設為 0。

它的真正含義是:系統先按照各控制項自己宣告的 layout_height(這裡是 wrap_content)把它們的高度分配好,然後把剩下來的螢幕空間,依權重比例發給有設 weight 的控制項。因為只有第二個 weight 是 1,所以整塊剩餘空間全歸它——這就是它看起來特別高的原因。

二、水平方向的例子

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:background="#ff0000"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="1"
        android:textColor="@android:color/white"
        android:layout_weight="1" />

    <TextView
        android:background="#cccccc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="2"
        android:textColor="@android:color/black"
        android:layout_weight="2" />

    <TextView
        android:background="#ddaacc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="3"
        android:textColor="@android:color/black"
        android:layout_weight="3" />

</LinearLayout>

三個 TextView 都是 wrap_content,結果如下:

依前面的理解:系統先給三個 TextView 分配足以容納「1」「2」「3」的寬度,再把剩餘空間按 1:2:3 分給它們,於是得到上圖。

三、把 layout_width 改成 match_parent 會發生什麼

你會發現 weight 最小的「1」反而佔最寬。網上常見的解釋是「match_parent 時 weight 越小權重越大」——這句話是結論,不是原因。真正的原因就寫在 layout_width="match_parent" 裡。我們用同一套算式推一遍:

系統先給每個 TextView 分配它們所要的寬度 match_parent,也就是每個都填滿父容器(這裡等於螢幕寬度)。此時:

剩餘空間 = 1 個 parent_width − 3 個 parent_width = −2 個 parent_width

注意這裡出現了負數。於是(weight 總和為 1+2+3=6):

  • 第一個實際寬度 = 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,第三個直接消失。這與圖上完全一致。

如果三個 weight 都設為 1(總和 3),算式變成:

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

也就是均分——這正是我們改用 weight 時最常期待的結果。

四、結論與實務建議

  • 權重描述的是「如何分配剩餘空間」,而不是「佔多少比例」。當剩餘空間為負,權重越大反而扣得越多,這就是所有反直覺現象的來源。
  • 想做等分,請用 0dp + weight:把 layout_width(水平排列時)設為 0dp,讓每個控制項的初始佔寬為 0,剩餘空間即為整個父容器寬度,此時 weight 才真正等於佔比。這是官方文件推薦、也是實務上最可靠的寫法。
  • wrap_content + weight 適合「內容寬度自適應,再把多餘空間給某幾個」的情境。
  • weight 之和不必是 1:系統會自動以總和做正規化,寫 1:2:3 和寫 0.1:0.2:0.3 效果相同。
  • 現代替代方案:需要等分時,LinearLayout 搭配 0dp+weight 仍然最直接;若是兩欄比例固定且較複雜,可考虑 ConstraintLayout 以 guideline 與 ratio 描述,版面層級也更扁平。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved