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

layout_weight 搭配 0px:最乾淨的 Android 多螢幕自適應方案

編輯:關於 Android 程式設計

在談自適應之前,先釐清 android:layout_weight 到底在分配什麼。它決定的是控制項相對於父佈局的顯示權重,通常用於線性佈局。值的意義需要分開理解:

  • 權重為 0(預設):控制項只佔 layout_width 或 layout_height 所宣告的大小
  • 權重大於 0:控制項參與「剩餘空間」的分配,數值越大分到的越多

橫向佈局中它影響的是 layout_width,縱向佈局中影響 layout_height。

傳統寫法的困擾

常見的教學是把 layout_width 與 layout_height 都設成 fill_parent,再交給 weight 決定比例。這麼做的副作用是:weight 越小,實際顯示比例越大——因為每個控制項都先宣稱要佔滿全寬,最後再按反比削減。兩個控制項時還算好算,一旦數量變多、比例又不規則,反比換算就非常容易出錯。

0px 寫法:讓比例直觀對應

把主軸方向上的尺寸設為 0px,再搭配 weight,控制項就會正比例顯示。weight = 1 與 weight = 3 就是 1:3,不必再做任何反比心算。這看似難以理解的寫法,正好解決了 Android 碎片化裡最惱人的一塊。

把樣式抽成 style 复用

與其每個控制項都寫一遍屬性,不如抽成四個 style(res/values/style_layout.xml):

<resources>
    <!-- 全螢幕拉伸 -->
    <style name="layout_full">
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">fill_parent</item>
    </style>

    <!-- 固定自身大小 -->
    <style name="layout_wrap">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
    </style>

    <!-- 橫向分布:寬度交給 weight -->
    <style name="layout_horizontal" parent="layout_full">
        <item name="android:layout_width">0px</item>
    </style>

    <!-- 縱向分布:高度交給 weight -->
    <style name="layout_vertical" parent="layout_full">
        <item name="android:layout_height">0px</item>
    </style>
</resources>

之後布局檔只需引用樣式,不必每個控制項重複宣告:

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    style="@style/layout_full"
    android:orientation="vertical">

    <!-- 第一列:橫向 1 : 4 : 3 : 2 -->
    <LinearLayout
        style="@style/layout_vertical"
        android:layout_weight="1"
        android:orientation="horizontal">
        <View style="@style/layout_horizontal" android:background="#aa0000" android:layout_weight="1" />
        <View style="@style/layout_horizontal" android:background="#00aa00" android:layout_weight="4" />
        <View style="@style/layout_horizontal" android:background="#0000aa" android:layout_weight="3" />
        <View style="@style/layout_horizontal" android:background="#aaaaaa" android:layout_weight="2" />
    </LinearLayout>

    <!-- 第二列:縱向 4 : 3 : 2 : 1 -->
    <LinearLayout
        style="@style/layout_vertical"
        android:layout_weight="2"
        android:orientation="vertical">
        <View style="@style/layout_vertical" android:background="#ffffff" android:layout_weight="4" />
        <View style="@style/layout_vertical" android:background="#aa0000" android:layout_weight="3" />
        <View style="@style/layout_vertical" android:background="#00aa00" android:layout_weight="2" />
        <View style="@style/layout_vertical" android:background="#0000aa" android:layout_weight="1" />
    </LinearLayout>

</LinearLayout>

這個結構的優點是:整體比例完全由 weight 表達,程式碼可讀性高,且不依賴任何絕對像素值,因此在不同螢幕尺寸與解析度上都能維持相同比例。

幾點補充

0px 這個寫法之所以可行,是因為 weight 分配的是「剩餘空間」,而把基準尺寸設為 0 之後,剩餘空間就等于父容器可用空間,比例自然為正。0dp 效果相同,兩者可以互換。

現代專案中,若需要更複雜的比例與定位關係,Constraint Layout 的 layout_constraintWidth_percent 與 guidelines 表達力更強,且能保持扁平層級。但單純的等分或固定比例分割,weight 加 0px 仍然是最直觀、效能最好的選擇。

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