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

Android 五大佈局完整整理:LinearLayout、RelativeLayout、FrameLayout、TableLayout 與 AbsoluteLayout

編輯:關於 Android 程式設計

Android 的介面是由佈局與組件協同完成的。佈局好比建築中的框架,組件則相當於磚瓦;組件依佈局的要求排列,就組成了使用者看到的畫面。

所有的佈局方式都可以歸類為 ViewGroup 的五個直接子類,其他佈局都是從這五種擴展而來。

1. LinearLayout:線性佈局

這是最常用也最簡單的一種。子元素依照垂直或水平方向依次排列,每個子元素位於前一個之後。垂直排列時是 N 列單欄結構,每一列只有一個元素,不論其寬度為何;水平排列時則是單列 N 欄結構。若想做出兩列兩欄的效果,一般做法是先垂直排列兩個元素,每個元素內部再各自包含一個水平排列的 LinearLayout。

子元素的 android:layout_weight 屬性用來描述它在剩餘空間中佔的比例。若一列只有一個文字方塊,預設值為 0;若一列有兩個等寬文字方塊,兩者的 weight 可以都設為 1;若希望比例為二比一,則分別設為 1 和 2,此時第一個佔剩餘空間的三分之二,第二個佔三分之一。原則是數值越小,重要性越高。

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

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#ff000000"
        android:text="Hello">

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

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="#ff654321"
                android:text="1" />

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="2"
                android:background="#fffedcba"
                android:text="2" />
        </LinearLayout>
    </TextView>
</LinearLayout>

2. RelativeLayout:相對佈局

RelativeLayout 依各子元素之間的相對位置關係來排版,因此與位置相關的屬性會生效,例如 android:layout_below、android:layout_above、android:layout_centerVertical 等。注意:指定位置關係時,被引用的 ID 必須先被定義,否則會出現異常。

RelativeLayout 是五大佈局中最靈活的一種,比較適合排版較複雜的畫面。

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

    <TextView
        android:id="@+id/text_01"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:background="#ffffffff"
        android:gravity="center"
        android:text="1" />

    <TextView
        android:id="@+id/text_02"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_above="@id/text_01"
        android:layout_centerHorizontal="true"
        android:background="#ff654321"
        android:gravity="center"
        android:text="2" />

    <TextView
        android:id="@+id/text_03"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_above="@id/text_01"
        android:layout_toLeftOf="@id/text_02"
        android:background="#fffedcba"
        android:gravity="center"
        android:text="3" />
</RelativeLayout>

3. AbsoluteLayout:絕對位置佈局

此佈局中,子元素的 android:layout_x 與 android:layout_y 屬性會生效,用來描述座標位置。螢幕左上角為原點 (0,0),第一個 0 是橫座標,向右增大;第二個 0 是縱座標,向下增大。子元素之間可以互相重疊。

實務開發中通常不採用這種方式,因為它的排版過於剛性,難以適配各種終端。

<TextView
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_x="50dp"
    android:layout_y="50dp"
    android:background="#ffffffff"
    android:gravity="center"
    android:text="1" />

<TextView
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_x="25dp"
    android:layout_y="25dp"
    android:background="#ff654321"
    android:gravity="center"
    android:text="2" />

<TextView
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_x="125dp"
    android:layout_y="125dp"
    android:background="#fffedcba"
    android:gravity="center"
    android:text="3" />

4. FrameLayout:框架佈局

FrameLayout 是五種佈局中最簡單的一種,也可以理解為「層疊」方式。整個畫面被當成一塊空白備用區域,所有子元素都放在左上角,後面的子元素會直接覆蓋在前面的子元素之上,將前者部分或全部遮擋。下例中,第一個 TextView 被第二個完全遮擋,第三個 TextView 又遮住了第二個的一部分。

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

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#ff000000"
        android:gravity="center"
        android:text="1" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#ff654321"
        android:gravity="center"
        android:text="2" />

    <TextView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="#fffedcba"
        android:gravity="center"
        android:text="3" />
</FrameLayout>

5. TableLayout:表格佈局

適用於 N 列 N 欄的排版。一個 TableLayout 由許多 TableRow 組成,一個 TableRow 就代表表格中的一列。

TableRow 是 LinearLayout 的子類。TableLayout 不需要明確宣告有幾列幾欄,而是透過 TableRow 與其他組件來控制表格的列數與欄數;每往 TableRow 裡新增一個組件,表格就多一欄。若直接把組件放進 TableLayout,該組件會自成一行。表格中各欄的寬度由該欄最寬的儲存格決定,整個表格的寬度則取決於父容器(預設佔滿父容器)。

由於 TableLayout 繼承自 LinearLayout,因此完全支援 LinearLayout 的所有 XML 屬性,此外還支援:

android:collapseColumns:設定要隱藏的欄序號,多個以逗號分隔。
android:shrinkColumns:設定要收縮的欄序號,多個以逗號分隔。
android:stretchColumns:設定允許被拉伸的欄序號,多個以逗號分隔。

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

    <TableRow>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#ffffffff"
            android:gravity="center"
            android:padding="10dp"
            android:text="1" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#ff654321"
            android:gravity="center"
            android:padding="10dp"
            android:text="2" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#fffedcba"
            android:gravity="center"
            android:padding="10dp"
            android:text="3" />
    </TableRow>

    <TableRow>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#ff654321"
            android:gravity="center"
            android:padding="10dp"
            android:text="2" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#fffedcba"
            android:gravity="center"
            android:padding="10dp"
            android:text="3" />
    </TableRow>

    <TableRow>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#fffedcba"
            android:gravity="center"
            android:padding="10dp"
            android:text="3" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#ff654321"
            android:gravity="center"
            android:padding="10dp"
            android:text="2" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#ffffffff"
            android:gravity="center"
            android:padding="10dp"
            android:text="1" />
    </TableRow>
</TableLayout>
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved