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

Android 五大版面配置詳解(附完整範例)

編輯:關於 Android 程式設計

寫 Android 介面,第一步永遠是選對版面配置。本文整理最常用的五種,逐一說明特性、常用屬性並附上可運行的範例。

線性版面(LinearLayout):依水平或垂直方向排列子元件。

框架版面(FrameLayout):所有子元件從左上角 (0,0) 開始堆疊。

表格版面(TableLayout):以列與行的方式排列子元件。

相對版面(RelativeLayout):依其他元件的相對位置定位。

絕對版面(AbsoluteLayout):以精確座標定位(2.0 之後已標示為過時)。

一、線性版面 LinearLayout

最常用的一種。透過 android:orientation 設定排列方向,可選 vertical(垂直)或 horizontal(水平)。

常用屬性:

android:orientation:排列方向。

android:gravity:控制子元件在版面內的對齊方式。

layout_weight:控制各元件在版面中的相對佔比。

範例一:表單與按鈕

<?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:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <EditText
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="right">   <!-- 按鈕靠右對齊 -->
        <Button android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="確定" />
        <Button android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="取消" />
    </LinearLayout>
</LinearLayout>

範例二:layout_weight 的分配規則

<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:background="#aa0000"
            android:gravity="center_horizontal"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1" />
        <TextView android:text="Teal"   android:background="#008080"
            android:gravity="center_horizontal"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1" />
        <TextView android:text="blue"   android:background="#0000aa"
            android:gravity="center_horizontal"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1" />
        <TextView android:text="orange" android:background="#FFA500"
            android:gravity="center_horizontal"
            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:background="#aa0000"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" />
        <TextView android:text="row two"   android:textSize="15pt"
            android:background="#DDA0DD"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" />
        <TextView android:text="row three" android:textSize="15pt"
            android:background="#008080"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" />
        <TextView android:text="row four"  android:textSize="15pt"
            android:background="#FFA500"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" />
    </LinearLayout>
</LinearLayout>

weight 的計算方式:layout_weight 是非負整數。版面會依「該元件的 weight ÷ 同一層級所有元件 weight 之和」的比例來分配空間。因此在水平 LinearLayout 中,兩個 weight 皆為 1 的按鈕會各佔一半寬度;weight 為 0 的元件維持原尺寸不被拉伸,其餘元件的可用空間則是「總空間減去 weight 為 0 者所佔部分」後再分配。

二、框架版面 FrameLayout

所有子元件都從螢幕左上角 (0,0) 開始堆疊。先加入的在下層,後加入的在上層,上層會覆蓋下層。常用於需要圖文疊放、或搭配 Fragment 動態切換的場景。

<FrameLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TextView android:layout_width="300dp" android:layout_height="300dp"
              android:background="#00BFFF" />
    <TextView android:layout_width="260dp" android:layout_height="260dp"
              android:background="#FFC0CB" />
    <TextView android:layout_width="220dp" android:layout_height="220dp"
              android:background="#0000FF" />
</FrameLayout>

三、表格版面 TableLayout

以表格形式排列子 View,透過 TableRow 定義每一列,元件落在哪一格由加入順序決定。

常用屬性:

android:collapseColumns:隱藏指定的列。

android:shrinkColumns:壓縮指定列以適應螢幕,不讓內容溢出。

android:stretchColumns:讓指定列盡量填滿剩餘空白。

android:layout_column:指定元件所在的列。

android:layout_span:指定元件跨越的列數。

<TableLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TableRow>
        <Button android:text="Button1" />
        <Button android:text="Button2" />
        <Button android:text="Button3" />
    </TableRow>

    <TableRow>
        <Button android:text="Button4" />
        <Button android:text="Button5" android:layout_span="2" />
    </TableRow>
</TableLayout>

四、相對版面 RelativeLayout

依元件之間的相對關係定位,例如「在 A 的右邊」「在 B 的下方」「靠父容器右側」。使用前提是:被參照的元件必須先設定 android:id,後面的元件才能用 @id/xxx 引用它。彈性最高,通常能以最淺的階層完成複雜介面,效能上也比多層嵌套來得好。

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="10px">

    <TextView
        android:id="@+id/tev1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="30dp"
        android:text="Please Type Here:" />

    <EditText
        android:id="@+id/tx1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/tev1" />

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/tx1"
        android:layout_alignParentRight="true"
        android:text="確定" />

    <Button
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/tx1"
        android:layout_toLeftOf="@id/btn1"
        android:layout_marginRight="30dp"
        android:text="取消" />
</RelativeLayout>

五、絕對版面 AbsoluteLayout

以精確的 X、Y 座標決定元件位置。Android 2.0 的 API 文件中已標示為 deprecated,建議改用 FrameLayout 或 RelativeLayout 取代——前者能達到疊放效果,後者能兼顧不同螢幕尺寸的適應性。絕對座標在不同解析度下幾乎必然錯位,因此新專案不應再使用。

選用建議

實務上多數介面會以 RelativeLayout 或 LinearLayout 為骨架,再視需要嵌套。原則是:層級越淺越好,因為版面量測與繪製的成本會隨嵌套深度上升;同時避開 RelativeLayout 的連續巢狀(它需要量測兩次才能定案),必要時改用 ConstraintLayout 會是更現代的選擇。

以上就是 Android 五大版面配置的整理,後續會再補充 ConstraintLayout 與自定義 ViewGroup 的相關內容。

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