萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> Android 開發

Android 主界面 XML 佈局最佳實踐:LinearLayout、SlidingDrawer、屬性配置與響應式設計指南

XML 佈局是 Android UI 開發的基石。本文以經典 main.xml 為例,深度解析 LinearLayout 垂直佈局、SlidingDrawer (已棄用但仍見於舊項目) 交互容器、核心屬性 (id、尺寸、背景、方向、手柄/內容綁定)、單位選擇 (dp/sp/px) 、響應式設計原則,並給出現代替代方案 (DrawerLayout, BottomSheet, CoordinatorLayout)。

一、 經典 main.xml 完整解析

<?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:idView 唯一標識@+id/name (生成新ID) / @id/name (引用現有) — **SlidingDrawer 的 handle/content 必須用 @id 引用**
layout_width/height佈局尺寸match_parent (填滿父) / wrap_content (包裹內容) / 固定 dp — **避免 px**
orientationLinearLayout/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 僅用於尺寸。

三、 單位選擇鐵律:dp vs sp vs px

  • dp (dip):密度無關像素。**所有尺寸 (寬高、邊距、padding、圓角、圖標大小) 必用 dp**。1dp = 1px @ 160dpi。保證不同密度屏幕物理尺寸一致。
  • sp:縮放無關像素。**所有文字大小必用 sp**。= dp × 用戶字體縮放比例。尊重用戶無障礙設置。
  • px:物理像素。**僅在代碼中動態計算、繪圖 Canvas、圖片像素操作時使用**。XML 禁用。

四、 SlidingDrawer 棄用原因與現代替代方案

棄用原因 (API 17):API 設計缺陷 — 不支持嵌套滑動、觸摸事件分發衝突、動畫僵硬、無 Material Design 風格、無法適配全面屏手勢/瀏海屏。
現代替代:

  1. DrawerLayout (側滑抽屜):androidx.drawerlayout.widget.DrawerLayout。支持左/右/雙側滑、邊緣滑動、鎖定模式、Material 風格、完美適配手勢導航。
    implementation "androidx.drawerlayout:drawerlayout:1.2.0"
  2. BottomSheetBehavior / BottomSheetDialogFragment (底部彈窗):com.google.android.material.bottomsheet.BottomSheetBehavior。從底部滑出、支持展開/收起/半展開狀態、手勢拖拽、鋪滿/適配內容高度。
    implementation "com.google.android.material:material:1.12.0"
  3. CoordinatorLayout + AppBarLayout + Behavior:複雜協同滑動 (ToolBar 隱藏/顯示、FAB 聯動、視差滾動)。
  4. Navigation Drawer (NavigationView + DrawerLayout):標準導航抽屜,Menu 資源驅動,支持 Header、項目圖標/標籤/徽章、選中狀態。

五、 響應式佈局進階:ConstraintLayout 扁平化

現代佈局**首選 ConstraintLayout** (或 Compose):

  • 扁平視圖層級 (無嵌套 LinearLayout/RelativeLayout),渲染性能優。
  • 相對約束 (鏈、比例、基線、圓周、Guideline/Barrier/GuideLine 輔助線) 實現複雜響應式佈局。
  • Layout Editor 可視化拖拽編輯,所見即所得。
  • 支持 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**,舊項目漸進式重構。

copyright © 萬盛學電腦網 all rights reserved