Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發 >> 開發入門

Android 五大基礎佈局完全解析:FrameLayout、LinearLayout、AbsoluteLayout、RelativeLayout、TableLayout

編輯:開發入門

Android 的版面系統由幾個基礎 Layout 組成,理解各自的特性與適用時機,是寫出好界面的第一步。以下逐一說明。

FrameLayout:最單純的堆疊容器

FrameLayout 是最簡單的佈局物件。你可以把它想像成螢幕上一塊空白的備用區域,之後往裡頭填入單一物件——比如一張要顯示的圖片。

它的特性是:所有子元素都會固定在螢幕左上角,你無法為 FrameLayout 的子元素指定位置。而且後加入的子元素會直接覆蓋在前者之上,把前面的元素部分或全部擋住(除非後者是透明的)。

<?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">

    <!-- 這裡放了一個 Button -->
    <Button
        android:text="button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:text="textView"
        android:textColor="#0000ff"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</FrameLayout>

LinearLayout:線性排列與 weight 權重

LinearLayout 會依你指定的 vertical 或 horizontal 屬性來排列所有子元素。子元素被依序堆疊,因此垂直列表的每一列只有一個元素(不論它多寬),而水平列表只有一個列高(等於最高子元素的高度加上邊框)。LinearLayout 也會維持子元素之間的間距與對齊方式(靠右、居中或靠左)。

它還支援為個別子元素指定 weight。這機制的價值在於允許子元素填滿螢幕上的剩餘空間,避免在大螢幕上一串小物件擠成一堆,而是讓它們按比例放大填補空白。子元素指定的 weight 值會決定剩餘空間的分配比例,預設值為 0。

舉例來說,若有三個文字方塊,其中兩個指定 weight 為 1,那麼這兩個會等比例放大並填滿剩餘空間,第三個則維持原尺寸不放大。

<?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="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="2">

        <TextView
            android:text="Welcome to Mr Wei's blog"
            android:textSize="15pt"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <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" />
    </LinearLayout>
</LinearLayout>

AbsoluteLayout:絕對座標(不建議使用)

AbsoluteLayout 讓子元素指定精確的 x/y 座標來定位,(0, 0) 為左上角,往右下移動時座標值變大。它沒有頁邊框,允許元素互相重疊。

官方並不鼓勵使用 AbsoluteLayout,除非你有正當理由。因為它把界面寫得過於剛性,換到不同解析度或不同尺寸的裝置上很可能就跑歪了。

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

    <EditText
        android:text="Welcome to Mr Wei's blog"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <Button
        android:layout_x="250px"
        android:layout_y="40px"
        android:layout_width="70px"
        android:layout_height="wrap_content"
        android:text="Button" />
</AbsoluteLayout>

RelativeLayout:相對定位,最靈活

RelativeLayout 允許子元素指定相對於其他元素或父元素的位置(透過 ID 參照)。因此你可以把兩個元素排成靠右對齊、上下相鄰,或置於畫面中央。

要特別注意的是它的排序依賴性:元素是按順序處理的,如果第一個元素置於畫面中央,那麼相對於它的其他元素就會以中央為基準來定位。因此在用 XML 撰寫時,被參照的元件必須先宣告,否則會找不到 ID。

<?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/label"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Welcome to Mr Wei's blog:"/>

    <EditText
        android:id="@+id/entry"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/label"/>

    <Button
        android:id="@+id/ok"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/entry"
        android:layout_alignParentRight="true"
        android:layout_marginLeft="10dip"
        android:text="OK" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/ok"
        android:layout_alignTop="@id/ok"
        android:text="Cancel" />
</RelativeLayout>

TableLayout:表格化排列

TableLayout 把子元素分配到列與行。它由多個 TableRow 組成,每個 TableRow 定義一列(事實上你也可以直接放入其他子物件)。

幾個容易搞混的特性:TableLayout 容器不會顯示 row、column 或 cell 的邊框線;每一列擁有 0 個或多個 cell,每個 cell 容納一個 View 物件;表格允許儲存格為空;而且儲存格不能跨列,這點和 HTML 的 table 不同。若要撐開某幾欄的寬度,可以透過 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"
    android:stretchColumns="1">

    <TableRow>
        <TextView android:layout_column="1" android:text="Open..." />
        <TextView android:text="Ctrl-O" android:gravity="right" />
    </TableRow>

    <TableRow>
        <TextView android:layout_column="1" android:text="Save..." />
        <TextView android:text="Ctrl-S" android:gravity="right" />
    </TableRow>

    <View android:layout_height="2dip" android:background="#FF909090" />

    <TableRow>
        <TextView android:text="X" />
        <TextView android:text="Export..." />
        <TextView android:text="Ctrl-E" android:gravity="right" />
    </TableRow>

    <View android:layout_height="2dip" android:background="#FF909090" />

    <TableRow>
        <TextView android:layout_column="1" android:text="Quit"
            android:padding="3dip" />
    </TableRow>
</TableLayout>

其中那兩個 View 就是畫面上的分隔線——一個高度 2dip、灰色背景的空 View,這種小技巧在實作清單型界面時很好用。

該選哪一個?

實務上的建議是:預設用 LinearLayout 處理線性排列、RelativeLayout 處理需要相對定位的複合畫面,FrameLayout 用在只需要堆疊的單純場景(例如自訂 Dialog 或浮層),TableLayout 用在本身就是表格語義的資料呈現。AbsoluteLayout 則能避就避。這幾種 Layout 也可以任意巢狀組合,靈活搭配才能構出複雜又maintainable的界面。

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