XML 佈局是 Android UI 開發的基石。本文以經典 main.xml 為例,深度解析 LinearLayout 垂直佈局、SlidingDrawer (已棄用但仍見於舊項目) 交互容器、核心屬性 (id、尺寸、背景、方向、手柄/內容綁定)、單位選擇 (dp/sp/px) 、響應式設計原則,並給出現代替代方案 (DrawerLayout, BottomSheet, CoordinatorLayout)。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:id="@+id/root_layout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#808080"
android:padding="16dp">
<!-- SlidingDrawer: 滑動抽屜 (API 17 棄用,建議用 DrawerLayout/BottomSheet) -->
<SlidingDrawer
android:id="@+id/sliding_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:handle="@+id/handle"
android:content="@+id/content"
android:topOffset="0dp"
android:allowSingleTap="true"
android:animateOnClick="true">
<!-- 手柄:用戶拖動/點擊的區域 -->
<Button
android:id="@id/handle"
android:layout_width="88dp"
android:layout_height="44dp"
android:background="@drawable/handle_bg"
android:text="拖動展開"
android:textColor="#FFFFFF"
android:gravity="center" />
<!-- 內容:抽屜展開顯示的區域 -->
<LinearLayout
android:id="@id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#FFFFFF"
android:padding="16dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="抽屜內容區域\n可放置設置、菜單、表單等"
android:textSize="16sp"
android:textColor="#333333"
android:lineSpacingExtra="4dp"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="確認操作"
android:backgroundTint="#2196F3"
android:textColor="#FFFFFF"/>
</LinearLayout>
</SlidingDrawer>
</LinearLayout>
| 屬性 | 作用 | 關鍵值/建議 |
|---|---|---|
android:id | View 唯一標識 | @+id/name (生成新ID) / @id/name (引用現有) — **SlidingDrawer 的 handle/content 必須用 @id 引用** |
layout_width/height | 佈局尺寸 | match_parent (填滿父) / wrap_content (包裹內容) / 固定 dp — **避免 px** |
orientation | LinearLayout/SlidingDrawer 排列/滑動方向 | vertical / horizontal |
background | 背景 (顏色/圖片/選擇器/形狀) | 顏色 #RRGGBB/#AARRGGBB、Drawables、@drawable/selector |
padding | 內邊距 (View 內部留白) | 統一 16dp (Material 標準),或分向 paddingStart/End/Top/Bottom |
layout_weight | 線性佈局權重分配剩餘空間 | 配合 layout_width="0dp" 實現比例分配 (如 1:2) |
gravity / layout_gravity | 內容對齊 / 自身在父中對齊 | center/center_vertical/center_horizontal/start/end |
textSize | 文字大小 | 必須用 sp (縮放像素),隨系統字體大小縮放。dp 僅用於尺寸。 |
棄用原因 (API 17):API 設計缺陷 — 不支持嵌套滑動、觸摸事件分發衝突、動畫僵硬、無 Material Design 風格、無法適配全面屏手勢/瀏海屏。
現代替代:
androidx.drawerlayout.widget.DrawerLayout。支持左/右/雙側滑、邊緣滑動、鎖定模式、Material 風格、完美適配手勢導航。implementation "androidx.drawerlayout:drawerlayout:1.2.0"com.google.android.material.bottomsheet.BottomSheetBehavior。從底部滑出、支持展開/收起/半展開狀態、手勢拖拽、鋪滿/適配內容高度。implementation "com.google.android.material:material:1.12.0"現代佈局**首選 ConstraintLayout** (或 Compose):
Flow (流式佈局)、MotionLayout (動畫/過渡)。<androidx.constraintlayout.widget.ConstraintLayout ...>
<TextView android:id="@+id/title" ...
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
<Button android:id="@+id/btn"
app:layout_constraintTop_toBottomOf="@id/title"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintWidth_percent="0.8"/>
</androidx.constraintlayout.widget.ConstraintLayout>
XML 佈局核心在於:**語義化結構 (ConstraintLayout 扁平化) + 密度無關單位 (dp/sp) + 系統組件優先 (DrawerLayout/BottomSheet) + 主題樣式分離 (styles/themes) + 工具屬性預覽 (tools:)**。
理解經典 LinearLayout/SlidingDrawer 的屬性邏輯,是遷移至現代佈局體系的基石。新項目**直接上 ConstraintLayout / Jetpack Compose**,舊項目漸進式重構。