編輯:關於 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 分給它們,於是得到上圖。

你會發現 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):
所以比例是 2:1:0,第三個直接消失。這與圖上完全一致。
如果三個 weight 都設為 1(總和 3),算式變成:
也就是均分——這正是我們改用 weight 時最常期待的結果。
0dp + weight:把 layout_width(水平排列時)設為 0dp,讓每個控制項的初始佔寬為 0,剩餘空間即為整個父容器寬度,此時 weight 才真正等於佔比。這是官方文件推薦、也是實務上最可靠的寫法。wrap_content + weight 適合「內容寬度自適應,再把多餘空間給某幾個」的情境。LinearLayout 搭配 0dp+weight 仍然最直接;若是兩欄比例固定且較複雜,可考虑 ConstraintLayout 以 guideline 與 ratio 描述,版面層級也更扁平。
Android 刪除 Preference 的正確姿势與源碼解析
用 Preference 搭設置界面很省事,一個 XML 就足夠。但想在某個時機動態移除其中一項時,很多人會發現刪不掉——而且往往找不到原因。本文從源碼層面把這件事講清楚
Android 對話框(Dialog)與自定義 Dialog 完整指南
Activity 提供了一套便利的對話框管理機制,能協助我們建立、保存與回復對話框,例如 onCreateDialog(int)、onPrepareDialog(int,
從零開始學 Android(十七):用嵌套布局實現計算器界面
所謂嵌套布局,就是在同一個布局文件中把多個布局容器互相套疊。結構示意如下: <LinearLayout android:layout_width=matc
深度改造 Android ListView:打造高效能瀑布流布局
這是 ListView 系列三部曲的最終篇。前兩篇我們已經從源碼層面透徹剖析了 ListView 的工作原理與常見問題,這一篇則要動手改造它,讓 ListView 能以瀑