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

Android Material Design 支援庫完整指南:核心元件與實作重點

編輯:關於 Android 開發

Android Material Design 支援庫使用指南

Design Support Library 是 Google 為了讓開發者能在舊版 Android 系統上導入 Material Design 體驗而提供的支援庫。透過它,即使目標裝置不是最新版本,也能使用現代化的 UI 元件與互動模式。

支援範圍與依賴設定

Design Support Library 可支援 Android 2.1 以上裝置。若在 Gradle 中引入,可加入:

compile 'com.android.support:design:22.2.0'

實際使用時,版本號可依專案需求調整。此外,使用時通常需要在版面配置中加入 app 命名空間:xmlns:app="https://schemas.android.com/apk/res-auto"。

主要元件總覽

此支援庫包含多個常用元件,例如 TextInputLayout、FloatingActionButton、Snackbar、TabLayout、NavigationView、CoordinatorLayout、AppBarLayout 與 CollapsingToolbarLayout。

FloatingActionButton

FloatingActionButton 是懸浮於介面上的圓形按鈕,通常用於提示主要操作。它繼承自 ImageView,因此可透過 src 或 setImageDrawable 設定圖示。常用屬性包括 fabSize、elevation 與 rippleColor。

TextInputLayout

TextInputLayout 用來包裝 EditText,提供浮動提示與錯誤提示效果。當輸入內容不符合規則時,可透過 setError 顯示錯誤訊息,並以 setErrorEnabled 控制是否啟用錯誤狀態。

Snackbar

Snackbar 提供輕量級的操作回饋,通常顯示在畫面底部,可包含一段文字與一個操作按鈕。它會在設定時間後自動消失,也可透過使用者操作提前關閉。

CoordinatorLayout

CoordinatorLayout 可視為增強版 FrameLayout,主要用於協調子 View 之間的互動。例如當 Snackbar 出現時,FloatingActionButton 可自動上移,避免遮擋訊息。

NavigationView

NavigationView 常用於建立抽屜式導覽選單,可搭配 DrawerLayout 使用。它能透過 headerLayout 設定頂部內容,也能透過 menu 資源定義選單項目,並監聽項目點擊事件。

TabLayout

TabLayout 提供頁籤切換能力,可搭配 ViewPager 使用。透過 setupWithViewPager,可讓頁籤與頁面彼此同步,提升使用者操作的一致性。

AppBarLayout 與 CollapsingToolbarLayout

AppBarLayout 可讓 Toolbar 等元件回應捲動事件,常見於需要視差或折疊效果的頁面。CollapsingToolbarLayout 則可讓標題在展開與折疊時呈現不同樣式,常見屬性包括 collapseMode 與 scrollFlags。

實作小提醒

使用 CardView 時,若卡片填滿父容器,陰影效果可能不明顯,建議將 margin 設定大於 cardElevation。使用 AppBarLayout 時,內容區建議搭配 NestedScrollView,以確保捲動動畫能正常觸發。

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