編輯:Android 開發實例
這一節把 Android 的五種基本版面容器一次講清楚:FrameLayout(框架版面)、LinearLayout(線性版面)、AbsoluteLayout(絕對版面)、RelativeLayout(相對版面)、TableLayout(表格版面)。
FrameLayout 相當於螢幕上一塊空白備用區,你可以在裡面填東西。所有子元素都固定在左上角,無法 individually 指定位置;後加入的子元素會直接疊在前一個上面,把它部分或全部遮住(除非後者是透明的)。適合做浮層、遮罩這類需求。
[效果圖]
<?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
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="button" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="textview"
android:textColor="#0000ff" />
</FrameLayout>
LinearLayout 依你設定的 orientation 把所有子元素排成一條直線。垂直排列時每一列只有一個元素,水平排列時只有一列。
它最實用的是 weight:指定權重後,子元素可以填滿剩下的空間,避免在大螢幕上一堆小物件擠成一團。預設權重為 0,也就是不放大。舉例來說,三個文字框中有兩個設 weight="1",這兩個就會等比例放大填滿剩餘空間,第三個維持原樣。
[效果圖]
<?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">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="2"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Welcome to Mr Wei's blog"
android:textSize="15pt" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#aa0000"
android:gravity="center_horizontal"
android:text="red" />
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#00aa00"
android:gravity="center_horizontal"
android:text="green" />
</LinearLayout>
</LinearLayout>
AbsoluteLayout 讓子元素指定精確的 x/y 座標,左上角為 (0, 0),往右下移動數值變大。它沒有內距,也允許元素重疊。
實務上不推薦使用:寫死座標的版面太過剛性,換到不同尺寸或密度的裝置上幾乎一定走位。除非有非常明確的理由,否則請改用 RelativeLayout 或 ConstraintLayout。
[效果圖]
<AbsoluteLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Welcome to Mr Wei's blog" />
<Button
android:layout_width="70px"
android:layout_height="wrap_content"
android:layout_x="250px"
android:layout_y="40px"
android:text="Button" />
</AbsoluteLayout>
RelativeLayout 讓子元素指定「相對於其他元素或父容器」的位置(透過 id 關聯),可以輕鬆做到靠右對齊、上下排列或置中。元素是按順序解析的,所以用 XML 撰寫時,被引用的元素必須先定義。
[效果圖]
<?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_alignParentRight="true"
android:layout_below="@id/entry"
android:layout_marginLeft="10dip"
android:text="OK" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@id/ok"
android:layout_toLeftOf="@id/ok"
android:text="Cancel" />
</RelativeLayout>
TableLayout 把子元素分配到列與欄。它由多個 TableRow 組成,每個 TableRow 定義一列,每列可擁有零個或多個 cell,每個 cell 放一個 View。容器不會顯示表格線;cell 可以是空的,但不能跨欄——這點與 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:gravity="right" android:text="Ctrl-O" />
</TableRow>
<TableRow>
<TextView android:layout_column="1" android:text="Save..." />
<TextView android:gravity="right" android:text="Ctrl-S" />
</TableRow>
<!-- 分隔線 -->
<View
android:layout_height="2dip"
android:background="#FF909090" />
<TableRow>
<TextView android:text="X" />
<TextView android:text="Export..." />
<TextView android:gravity="right" android:text="Ctrl-E" />
</TableRow>
<View
android:layout_height="2dip"
android:background="#FF909090" />
<TableRow>
<TextView
android:layout_column="1"
android:padding="3dip"
android:text="Quit" />
</TableRow>
</TableLayout>
實務上的優先順序是:ConstraintLayout(能處理絕大多數版面,層級最淺)→ LinearLayout(單軸排列、權重分配)→ RelativeLayout(需要相對定位時)→ FrameLayout(單一子元素或堆疊場景)→ TableLayout(確實是表格資料時)。AbsoluteLayout 已標記為 deprecated,除非維護舊專案,否則不必再學。
版面層級越深,measure 與 layout 的開銷越大,這也是「能用 ConstraintLayout 平鋪就不用多層嵌套」的原因。
Android 通用流行開源框架大全整理
好的框架能讓開發事半功倍。本文按功能分類,整理了 Android 開發中最值得關注的一批流行開源框架,涵蓋緩存、圖片、網路、資料庫、依賴注入等各個領域,是一份值得收藏的速
Android 進度條教學:用 Circular Spinner 表達不確定進度
當 App 要執行一段耗時工作,使用者需要一個「你正在忙」的視覺回饋,這就是進度條存在的意義。Android 的 ProgressBar 有兩種常見形態:橫向的進度條(p
Android Studio 整合 OpenCV 開發環境的完整建置流程
前言 想在 Android 專案裡使用 OpenCV 進行影像處理或人臉偵測,第一步永遠是「把開發環境搭起來」。本文以 Android Studio 為主,示範如何從官方
Android 輕鬆實作淘寶風格地區選擇
淘寶在編輯地址時的地區選擇,最大特色是列表滾動時,新項目會從下方滑入並貼近上一項。本文以自製 Demo 重現這種效果,適合想替表單加入更細膩動畫的 Android 開發者