編輯:關於 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。
白話來說:系統先依 wrap_content 把三顆按鈕的高度定下來,然後把剩下的螢幕空間全給權重不為 0 的 button2。這就是它看起來特別「胖」的原因。
<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 時代表「優先盡可能小」。口訣好背,但忘記時就麻煩了,我們直接算一次。
match_parent,也就是每個都佔滿整個螢幕寬度(記為 parent_width)。3/5 : 1/5 : 1/5 就是 3:1:1,完全對得上。
那如果權重改成 1、2、3 呢?第三個 TextView 會直接消失。同樣算一次:總權重 6,剩餘空間仍是 −2 parent_width。
比例是 2:1:0,第三個連一點空間都分不到,自然看不見了。
懂了這個計算方式,之後遇到 match_parent 與 wrap_content 混用的複雜版面,都能自己算出結果,不必再靠試誤。
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 更有效率,因為可以少做一次不必要的計算。
Android 屬性動畫入門:從 ObjectAnimator 到自訂 TypeEvaluator
網路上高手的開場白往往先談心境、聊生活,我這裡就省掉鋪墊,直接進主題——因為這東西本來就不難。 動畫的兩套體系 Android 的動畫效果繞不開四件事:位移、縮放、旋轉、
Android ListView 靜態資料設定完整教學
有些時候,ListView 只需要呈現固定資料,不必從網路或資料庫動態取得。本文示範如何以靜態陣列建立圖文列表,並透過自訂 Adapter 完成顯示。 版面設計 主頁面
Glide 圖片加載庫:讓列表圖片平滑滾動
Glide 是 Android 上的圖片加載與快取庫(前身是 Google 在 I/O 大會展示的 Galleries 3.0 專案),核心目標只有一個:讓滾動的圖片列表
我的 Android 進階之旅——查看應用簽名信息
一、查看自己的證書簽名信息 如上一篇《我的 Android 進階之旅——製作和查看自訂的 Debug 版本簽名證書》所述,我們可以透過 keytool 命令查看生成的 k