Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux 程式設計 >> Linux 程式設計

Android 五大布局入門:LinearLayout 屬性詳解與官方示例

一個 Android 界面上往往擺着很多控件,它們的位置與排列由容器(Layout)來控制,作用類似於 HTML 裡的 div 與 table。

Android 常見的布局有五種:LinearLayout、RelativeLayout、TableLayout、FrameLayout 與 AbsoluteLayout。其中 AbsoluteLayout 靠指定 x/y 坐標定位,缺乏自適應能力,早已不推薦使用。

LinearLayout 的核心屬性

Orientation(方向):決定子控件排成一行還是一列,取值 horizontal 或 vertical,如 android:orientation="vertical",也可以在代碼中通過 setOrientation() 設置。

Fill Mode(填充方式):所有子控件都必須指定寬高,有三種取值——具體像素值(如 20px)、wrap_content(按內容實際尺寸)、match_parent(填充剩餘可用空間,舊版本寫作 fill_parent)。

Weight(權重):想讓同一行或同一列中的控件按比例分配空間時使用。例如希望某個控件佔據另一個兩倍的空間,把前者設為 1、後者設為 2 即可。注意權重只在 layout_width(水平方向)或 layout_height(垂直方向)為 0dp 或 match_parent 時才按預期工作。

官方示例解析

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <!-- 橫向的一行 -->
    <LinearLayout android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1">
        <TextView android:text="red" android:gravity="center_horizontal"
            android:background="#aa0000"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1" />
        <TextView android:text="green" android:gravity="center_horizontal"
            android:background="#00aa00"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1" />
        <TextView android:text="blue" android:gravity="center_horizontal"
            android:background="#0000aa"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1" />
        <TextView android:text="yellow" android:gravity="center_horizontal"
            android:background="#aaaa00"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1" />
    </LinearLayout>

    <!-- 豎向的四行 -->
    <LinearLayout android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1">
        <TextView android:text="row one" android:textSize="15pt"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" />
        <TextView android:text="row two" android:textSize="15pt"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" />
        <TextView android:text="row three" android:textSize="15pt"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" />
        <TextView android:text="row four" android:textSize="15pt"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" />
    </LinearLayout>
</LinearLayout>

仔細看這個結構:外層是垂直的 LinearLayout,內部嵌套了一個水平布局與一個垂直布局,兩者各帶 layout_weight="1",因此各佔屏幕一半高度;水平布局裡的四個 TextView 權重相同,平分寬度;垂直布局裡的四行 similarly 平分高度。這就是權重屬性最直觀的用法。

實戰建議:層級越少性能越好,能用 RelativeLayout 或後來的 ConstraintLayout 一層搞定的,就不要嵌套多層 LinearLayout,過深的嵌套會在 measure 階段產生指數級的開銷。

Copyright © Linux教程網 All Rights Reserved