編輯:關於 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>
這次成功:

同樣是設權重,設在哪個控件上就體現出理解深淺了。
我之前的理解一直是:把某個控件的寬或高設為 0dp,再給它權重 1,它就會填滿所有剩余空間。這個理解在很多場景下確實奏效,但它掩蓋了真正的機制。
layout_weight 表示的是對「剩余空間」的佔有比例。 我再強調一次,是「剩余」。
既然是挑剩下的,那自然應該優先讓其他控件先把自己需要的位置佔掉。所以回到開篇那個問題——想讓時間永遠顯示,就該把權重給那個「可以犧牲」的控件,讓它去適應剩余空間,而不是給必須顯示的時間。

這是為了屏幕適配而給所有子控件分配權重的典型寫法:
<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 是關鍵,因為剩餘空間等於全部空間。

仍然是那句話——layout_weight 分配的是剩餘空間。
假設兩個按鈕各自測量寬度都是 2,父容器寬度為 10,那麼剩余空間是 10 − 2 − 2 = 6:
button1 實際寬度 = 2 + 6 × (2/3) = 6
button2 實際寬度 = 2 + 6 × (1/3) = 4
<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 分配的是剩余空間,優先級最低。 想清楚這一點,再回頭看任何一個佈局,權重該放在誰身上就一目了然了。
Qt on Android 實踐:通過 JNI 調用系統原生分享菜單
原文《Sharing with Qt on Android》發表於 2014 年 12 月 12 日。當時我寫的遊戲 GiraffPanic(一款用 Qt & Q
Android ListView 與 GridView 拖拽移位實作
ListView 與 GridView 的拖曳排序,核心概念是攔截觸控事件、建立拖曳影像、計算目標位置,最後更新資料順序。這種互動常見於桌面、書籤、播放清單與設定頁。 L
Android Studio必備設置與高效操作技巧一覽
熟練掌握開發工具,往往能事半功倍,為我們省下大量重複勞動。今天就把Android Studio裡那些值得一開的設置與常用操作整理出來,幫助你把IDE調校得更順手。 一、
AppBarLayout + TabLayout + RecyclerView + ViewPager 實作可協同滑動的頭部
有人丟來一張效果圖,希望做出一個「Banner 捲出畫面後,標題欄吸頂」的頁面。仔細看起來不難,但要把 Material Design 那套協同滑動的規則串起來,還是得先