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

實習踩坑筆記:一個 UI Bug 讓我重新理解 layout_weight

編輯:關於 Android 程式設計

問題現場

今天改一個佈局問題時,才發現自己對權重的理解遠比想像中膚淺。

需求很簡單:一個 TextView 沒有按預期單行顯示,被擠成了兩行。

三次嘗試

第一個念頭是把左側文字的字級調小。師兄直接否決——不能隨意改動佈局尺寸,否則測試會再提 bug。

於是走第二條路:既然不能動字級,那就調邊距。圖片的 margin、TextView 的 margin 統統試了一遍。結果不用說,改動範圍這麼大更不可能通過。

師兄看我實在遲鈍,點了一句:不改佈局尺寸,怎麼讓時間無論如何都顯示出來?

我的做法是給時間那個 TextView 加上 layout_width="0dp" 與 layout_weight="1":

<TextView
    android:id="@+id/teacher_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="張文欣老師回答了您的問題"
    android:textColor="@color/color_blackest_text"
    android:textSize="@dimen/text_size_mediumer">

    <TextView
        android:id="@+id/notice_time"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="3dp"
        android:layout_weight="1"
        android:singleLine="true"
        android:text="5-16 4:04"
        android:textSize="@dimen/text_size_smaller" />
</TextView>

顯示結果:

還是原樣。奇怪了,設 layout_weight="1" 不就是為了保證它無論如何都顯示嗎?

師兄實在看不下去,親手示範:

<TextView
    android:id="@+id/teacher_name"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:singleLine="true"
    android:text="張文欣老師回答了您的問題"
    android:textColor="@color/color_blackest_text"
    android:textSize="@dimen/text_size_mediumer">

    <TextView
        android:id="@+id/notice_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="3dp"
        android:text="5-16 4:04"
        android:textSize="@dimen/text_size_smaller" />
</TextView>

這次成功:

關鍵認知:weight 分配的是「剩余空間」

同樣是設權重,設在哪個控件上就體現出理解深淺了。

我之前的理解一直是:把某個控件的寬或高設為 0dp,再給它權重 1,它就會填滿所有剩余空間。這個理解在很多場景下確實奏效,但它掩蓋了真正的機制。

layout_weight 表示的是對「剩余空間」的佔有比例。 我再強調一次,是「剩余」。

既然是挑剩下的,那自然應該優先讓其他控件先把自己需要的位置佔掉。所以回到開篇那個問題——想讓時間永遠顯示,就該把權重給那個「可以犧牲」的控件,讓它去適應剩余空間,而不是給必須顯示的時間。

四種組合下的實際表現

情況一:多個控件都設權重,寬度皆為 0dp

這是為了屏幕適配而給所有子控件分配權重的典型寫法:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <Button android:id="@+id/button1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:text="1"
        android:textSize="20sp" />

    <Button android:id="@+id/button2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="2"
        android:textSize="20sp" />
</LinearLayout>

button1 佔 2/3,button2 佔 1/3。這裡寬度設為 0dp 是關鍵,因為剩餘空間等於全部空間。

情況二:寬度改為 wrap_content

仍然是那句話——layout_weight 分配的是剩餘空間。

假設兩個按鈕各自測量寬度都是 2,父容器寬度為 10,那麼剩余空間是 10 − 2 − 2 = 6:

button1 實際寬度 = 2 + 6 × (2/3) = 6

button2 實際寬度 = 2 + 6 × (1/3) = 4

情況三:寬度改為 match_parent

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <Button android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:text="1"
        android:textSize="20sp" />

    <Button android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="2"
        android:textSize="20sp" />
</LinearLayout>

比例反過來了!權重大的 button1 反而佔得更少。原因在於:

兩個按鈕都設 match_parent,理論寬度都是 10,加起來 20,超出了父容器的 10。當子控件佔據的空間總和超過父容器時,超出的部分會按權重比例從各自原有空間中扣除。

button1 實際寬度 = 10 − (10 + 10 − 10) × 2/3 = 10 − 20/3 ≈ 1/3 個空間

button2 實際寬度 = 10 − (10 + 10 − 10) × 1/3 = 10 − 10/3 ≈ 2/3 個空間

這就解釋了那個反直覺的現象:為什麼有時候 weight 值大的控件反而佔得更小。

結論

權重實際佔到的比例,與對應的寬度或高度設定密切相關,必須結合 0dp、wrap_content、match_parent 三種情況分別判斷,不能死記硬背。

唯一永恆不變的規則只有一句:layout_weight 分配的是剩余空間,優先級最低。 想清楚這一點,再回頭看任何一個佈局,權重該放在誰身上就一目了然了。

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