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

Android 元件長寬比重設定總結:layout_width、layout_height 與 weight

編輯:關於 Android 程式設計

Android 的每個元件幾乎都有 android:layout_height、android:layout_width、android:layout_weight 這三個屬性。看似簡單,但 weight 的行為很容易讓人困惑。今天就把版面配置的相關規則整理成一篇總結。

各屬性可設定的值

android:layout_height / android:layout_width:

  • wrap_content:依內容自適應
  • match_parent(舊名 fill_parent):填滿父容器
  • 絕對單位:px(像素)、dp(密度獨立像素)、sp(依使用者偏好字級縮放)、in(英吋)、mm(毫米)

android:layout_weight:整數(可為小數)

實驗設計

想像一個橫向的 LinearLayout 要放三個元件。每個元件的寬度可以是「會變的」(如 TextView 隨文字長度伸縮)或「固定的」(如設定 50dp 的 ImageView)。三個位置各放一種元件,就有八種組合,分別觀察三個屬性的設定結果。

先記住一個前提:同一個 LinearLayout 中若不設比重,它會先滿足第一個元件的長與寬,不管後面還有沒有空間。這正是需要 weight 的原因。

案例一:三個都會變

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="阿斯頓發薩達發薩達發薩達發射的阿斯頓發阿斯頓發" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="阿斯頓發薩達發薩達發薩達發射的阿斯頓發阿斯頓發" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="阿斯頓發薩達發薩達發薩達發射的阿斯頓發阿斯頓發" />
</LinearLayout>

三個等重的 TextView,內容長度相近,看起來像是平均分。但仔細看會發現:weight 相同不代表寬度相同,它分配的是「剩餘空間」,而不是「全部空間」。

案例二:變變不(圖片寬度不在比重分配範圍內)

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:text="1的阿斯頓發阿sadfasdfasdfsadfsadfsd斯頓發" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="3"
        android:text="2頓發阿斯asdfa sdfadsfsdfasdfasdfasdfadsf頓發" />

    <ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:src="@drawable/jiantou1" />
</LinearLayout>

固定寬度的 ImageView 先拿走 50dp,剩下的空間才由兩個 TextView 依 1:3 分配。

案例三至八:其他排列組合

變不變、變不不、不不不、不不變、不變不、不變變——把會變與固定的元件換位置、換排列,就會發現:weight 的分配永遠發生在「扣除固定尺寸之後」的剩餘空間上,而元件在 LinearLayout 中的宣告順序會影響誰先取得空間。

把固定寬度的 ImageView 放在中間或最後,視覺上的比例也會跟著改變,這也是許多人搞不清 weight 的原因——它跟順序有關。

規律總結

仔細觀察的話,規律其實很清楚:

當 layout_width 設為 fill_parent(match_parent)時,layout_weight 代表你的元件要「優先盡可能地大」,但這個大有限度,就是 fill_parent。

當 layout_width 設為 wrap_content 時,layout_weight 代表你的元件要「優先盡可能地小」,但這個小有限度,就是 wrap_content。

layout_height 的規則與 layout_width 完全相同。

換個角度說:weight 低的元件會先被壓縮到最小(wrap_content)或撐到最大(match_parent),把剩餘空間讓給 weight 高的。

最推薦的寫法:0dp + weight

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

    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:text="2頓發阿斯asdfa ssdfsdgsdfgdfadsfsdfasdfasdfasdfadsf頓發" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:text="2頓發阿斯asdfa ssdfsdgsdfgdfadsfsdfasdfasdfasdfadsf頓發" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:text="2頓發阿斯asdfa ssdfsdgsdfgdfadsfsdfasdfasdfasdfadsf頓發" />
</LinearLayout>

在水平的 LinearLayout 中設定 layout_weight 時,把元件的 layout_width 設為 0dp,效果等同 wrap_content,但行為更可預期——它會嚴格按比例分配「扣除固定尺寸後所剩餘的空間」。

為什麼 0dp 才是正解?因為 wrap_content 會讓元件先依內容量測一次,再參與比重分配,等於量了兩次;而 0dp 直接跳過第一次量測,一次到位。元件越多、內容越複雜,這個差異越明顯。這是效能與可預期性的雙重收益。

底層計算邏輯

當 View 的 width 或 height 為 wrap_content 或 0dp/px/sp 時,若設定了 weight,layout 就會從剩餘未確定的空間中,依 weight 的總和(weightSum)計算要為這個 View 分配多少長寬。

width = leftWidth * weight / weightSum;

height = leftHeight * weight / weightSum;

另外,weight 的計算與版面配置,都發生在 LinearLayout 的 onMeasure 與 onLayout 函式中。

理解這一點之後,就能解釋所有看似奇怪的現象。例如「為什麼我設了 weight 卻沒效果」——通常是因為該方向的父容器尺寸是 wrap_content,導致 leftWidth 為 0,任何比例乘上 0 都還是 0。weight 要生效,父容器在該軸向必須有確定的尺寸。

最後提醒:在 android:orientation="vertical" 的 LinearLayout 中,weight 作用於高度;水平時才作用於寬度。搞錯方向是最常見的低級錯誤。若需要更複雜的比例配置,可考慮 ConstraintLayout 的 layout_constraintWidth_percent 或 Guideline,它用扁平化結構取代巢狀 LinearLayout,效能表現更好。

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