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

LinearLayout 完全攻略:常用屬性、layout_weight 權重與分割線實踐

編輯:關於 Android 程式設計

在 Android 的眾多布局中,LinearLayout(線性布局)大概是最早接觸、也最常被使用的一個。它把子控件按單一方向排成一列,看似簡單,但真正用好它,需要理解 orientation、gravity、layout_weight 這幾個關鍵屬性,尤其是權重分配背後的計算規則。下面我們把這些內容系統梳理一遍。

一、先看七個最常用的屬性

1. orientation——決定子元素的排列方向,取值為 horizontal(水平)或 vertical(垂直,默認值)。

2. gravity——控制布局內部所有子元素整體的對齊方式,支持多值組合,例如 center、right|bottom 等。

3. layout_gravity——控制「這個控件自己在父容器中」的對齊方式。

4. layout_width——寬度。實戰中很少寫死具體數值,通常用 wrap_content(包裹內容)或 match_parent(填滿父容器)。

5. layout_height——高度,取值邏輯同上。

6. id——為控件分配一個資源標識,之後在 Java 代碼裡就能用 findViewById 拿到它。

7. background——設置背景,可以是圖片,也可以直接是一塊純色。

gravity 與 layout_gravity 到底差在哪

這兩個名字極像的屬性,作用方向完全不同:android:gravity 管的是「容器對內部子元素」的對齊,android:layout_gravity 管的是「子元素在容器裡」的對齊。一句話:前者由父親決定孩子怎麼站,後者由孩子決定自己在父親心裡的位置。

二、layout_weight:屏幕適配的利器

layout_weight 用來給同一個 LinearLayout 中的多個子控件分配「重要度」。每個 View 的 weight 默認是 0,代表「內容需要多少空間,就佔多少空間」;一旦賦予大於 0 的值,父容器就會把剩餘可用空間按比例切分給它們。

用法一:最直觀的等分與比例分配

把兩個子 Layout 的 layout_width 設為 0dp,再分別給 weight 1 和 2,它們就會按 1:2 瓜分整行寬度:

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

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#ADFF2F" />

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:background="#DA70D6" />
</LinearLayout>

關鍵點:參與權重分配的控件,在被分配的那個方向上把尺寸寫成 0dp,這樣比例才會精確可控,也不會因為內容長度不同而互相影響。

用法二:wrap_content 也能用 weight

並不是必須寫 0dp。當三個 TextView 的 layout_width 都是 wrap_content、weight 依次為 1:2:3 時,效果如下:

此時系統會先按內容量測出每個控件的固有寬度,再把剩余空間按比例補發給它們,因此看起來並不是嚴格的 1:2:3。順帶一提,如果把 orientation 改成 vertical,由於水平方向不再受 weight 約束,第三個控件可能就擠到看不見了。

用法三:match_parent 的「意外」結果

三個控件寬度都寫 match_parent、weight 為 1:2:3,最終畫面上卻只看到兩個,比例還變成了 2:1:0。原因可以用一段簡單推演解釋:

1)每個 child 的建議寬度都是一整個屏幕,等於 1 個 match_parent,總需求為 3 個屏幕寬,於是剩餘空間變成 1 − 3 = −2 個 match_parent,是個負值;

2)各自應分到的份額依次為 1/6、2/6、3/6;

3)按比例把「負空間」分下去:第一個得 1 − 2×(1/6) = 2/3 屏;第二個得 1 − 2×(2/6) = 1/3 屏;第三個得 1 − 2×(3/6) = 0;

4)於是最終呈現 2:1:0——第三個控件被壓成了零寬度,自然「消失」了。

在 Java 代碼裡動態設置權重

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.MATCH_PARENT,
        LinearLayout.LayoutParams.WRAP_CONTENT);
lp.weight = 3;
view.setLayoutParams(lp);

三、給 LinearLayout 加分割線

做法一:手動塞一個「占位 View」。本質就是畫一條細線,簡單粗暴且無需任何素材:

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="2"
    android:background="#DA70D6">

    <View
        android:layout_width="match_parent"
        android:layout_height="1px"
        android:background="#000000" />
</LinearLayout>

background 也可以換成一張細線圖片,效果類似。

做法二:直接用 divider 系列屬性,需要準備一張線條圖片:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:divider="@drawable/henxian"
    android:dividerPadding="10dp"
    android:showDividers="middle">

    <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" />
    <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" />
    <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" />
</LinearLayout>

三個屬性分工明確:divider 指定分割線圖像,dividerPadding 控制分割線的內邊距,showDividers 決定分割線出現的位置——none(不顯示)、beginning(開頭)、end(末尾)、middle(各子控件之間)。

四、一個稍複雜的實例:嵌套出計算器鍵盤

LinearLayout 允許嵌套,這正是它能拼出複雜界面的原因。下面用「垂直外層 + 多個水平內層」的思路,做一個計算器按鍵區:

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

    <EditText
        android:id="@+id/msg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

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

        <Button android:layout_width="0dp" android:layout_height="wrap_content"
                android:layout_weight="1" android:text="mc" />
        <Button android:layout_width="0dp" android:layout_height="wrap_content"
                android:layout_weight="1" android:text="m+" />
        <Button android:layout_width="0dp" android:layout_height="wrap_content"
                android:layout_weight="1" android:text="m-" />
        <Button android:layout_width="0dp" android:layout_height="wrap_content"
                android:layout_weight="1" android:text="mr" />
    </LinearLayout>
    <!-- 其餘各行的組織方式完全相同,這裡省略 -->
</LinearLayout>

小結

LinearLayout 的學習重點其實就三件事:方向由 orientation 決定,對齊由 gravity 與 layout_gravity 分工負責,比例由 layout_weight 掌控。把「參與權重的方向寫 0dp」這個習慣養成,絕大多數適配問題都會迎刃而解;而嵌套的層級不宜過深,否則性能與可維護性都會打折。

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