編輯:關於 Android 程式設計
寫 Android 介面,第一步永遠是選對版面配置。本文整理最常用的五種,逐一說明特性、常用屬性並附上可運行的範例。
線性版面(LinearLayout):依水平或垂直方向排列子元件。
框架版面(FrameLayout):所有子元件從左上角 (0,0) 開始堆疊。
表格版面(TableLayout):以列與行的方式排列子元件。
相對版面(RelativeLayout):依其他元件的相對位置定位。
絕對版面(AbsoluteLayout):以精確座標定位(2.0 之後已標示為過時)。
最常用的一種。透過 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>

<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 者所佔部分」後再分配。
所有子元件都從螢幕左上角 (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>
以表格形式排列子 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>
依元件之間的相對關係定位,例如「在 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>
以精確的 X、Y 座標決定元件位置。Android 2.0 的 API 文件中已標示為 deprecated,建議改用 FrameLayout 或 RelativeLayout 取代——前者能達到疊放效果,後者能兼顧不同螢幕尺寸的適應性。絕對座標在不同解析度下幾乎必然錯位,因此新專案不應再使用。
實務上多數介面會以 RelativeLayout 或 LinearLayout 為骨架,再視需要嵌套。原則是:層級越淺越好,因為版面量測與繪製的成本會隨嵌套深度上升;同時避開 RelativeLayout 的連續巢狀(它需要量測兩次才能定案),必要時改用 ConstraintLayout 會是更現代的選擇。
以上就是 Android 五大版面配置的整理,後續會再補充 ConstraintLayout 與自定義 ViewGroup 的相關內容。
Android 內存洩露全解析:成因、典型場景與檢測排查
什麼是內存洩露? Android 虛擬機的垃圾回收採用的是根搜索算法。GC 會從根節點(GC Roots)出發遍歷整個 heap,最終那些無法直接或間接引用到 GC Ro
Android 自定義動畫:alpha、scale、translate、rotate、set 的屬性與用法全解
一、概述 Android 的 animation 由四種類型組成:alpha、scale、translate、rotate,對應官方文檔《Animation Resour
通過 Android 源碼分析再探觀察者模式(二):以 ListView 為例
接續上篇文章,這次我們透過 Android 實際開發與源碼來再探觀察者模式。ListView 是我們日常開發中必用的控件,雖然之前就有它的替代品 RecyclerView
Android ListView 元件解析:結構、XML 屬性與核心方法
本文整理 Android 中 ListView 的關鍵 API,適合在查閱官方文件時快速定位重點。一、類別結構ListView 是 Android 中常見的列表容器,其繼