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

Android Design Support Library 元件使用詳解

編輯:關於 Android 程式設計

概述

Android Design Support Library 是 Google 為支援 Material Design 而推出的元件庫,提供 Snackbar、FloatingActionButton、TextInputLayout、TabLayout、AppBarLayout、CollapsingToolbarLayout、NavigationView 與 CoordinatorLayout 等常用元件。它支援 Android 2.1 以上版本,能讓舊版系統也呈現較現代化的 MD 風格。

使用前可在 build.gradle 中加入 compile 'com.android.support:design:23.3.0'。

Snackbar

Snackbar 提供輕量級操作回饋,會從畫面底部短暫顯示訊息,可包含一個操作按鈕。它比 Toast 更靈活,可設定顯示時間、點擊事件、文字與背景顏色。

FloatingActionButton

FloatingActionButton 是懸浮操作按鈕,用於強調目前畫面最重要的動作。它繼承自 ImageButton,可設定背景色、陰影、大小與點擊事件。若與 Snackbar 同時使用,建議搭配 CoordinatorLayout,讓按鈕能為 Snackbar 讓出空間。

TextInputLayout

TextInputLayout 會將 EditText 包裹其中,讓 hint 在輸入時變成上方浮動標籤,並提供錯誤訊息顯示能力。它只接受一個 EditText 子元素,可透過 getEditText 取得輸入框,也可使用 setError 與 setErrorEnabled 控制錯誤提示。

TabLayout

TabLayout 提供水平頁籤版面,可與 ViewPager 搭配,形成常見的頂部分頁效果。可設定固定模式或可捲動模式,也能調整指示器、選中與未選中文字顏色。與 ViewPager 整合時,建議先設定 Adapter,再呼叫 setupWithViewPager,避免標題被清空。

AppBarLayout

AppBarLayout 是垂直 LinearLayout,常用於實作 Toolbar 隨捲動收合的效果。它需要作為 CoordinatorLayout 的直接子檢視,並透過 layout_scrollFlags 指定捲動行為。常見組合是 CoordinatorLayout、AppBarLayout、Toolbar 與可捲動內容。

CollapsingToolbarLayout

CollapsingToolbarLayout 可包裹 Toolbar,實作可折疊的 App Bar。它能包含視差背景圖,並在收合時改變標題樣式。常用屬性包括 contentScrim、expandedTitleMarginStart、layout_scrollFlags、layout_collapseMode 與 layout_collapseParallaxMultiplier。

CoordinatorLayout

CoordinatorLayout 是強化版 FrameLayout,能協調子檢視之間的互動。它可讓 FloatingActionButton 在 Snackbar 出現時上移,也能與 AppBarLayout 搭配,完成捲動連動效果。

NavigationView

NavigationView 提供標準化的抽屜導覽選單,可透過 headerLayout 與 menu 資源描述頂部與選單內容。它通常放在 DrawerLayout 中,並透過 setNavigationItemSelectedListener 處理選單點擊,同時可自訂文字與圖示顏色。

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