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

Android 5.0 的 Material Design:從主題到新控件

編輯:關於 Android 程式設計

Google 推出的 Material Design 設計規範,把扁平化的視覺語言與明亮配色結合在一起,配合層次分明的材質感,讓應用界面呈現出一種煥然一新的質感。

Material Design 直譯為「材料設計」,核心思想是通過多種元素的組合營造層次感:新的主題體系、新的控件、新的動畫。使用它時首先要注意兼容性——需要覆蓋市場佔有率仍然可觀的低版本系統。

一、配置 Material 主題

使用 Material Design 的第一步是配置對應主題,共有三個基礎選項:

android:Theme.Material
android:Theme.Material.Light
android:Theme.Material.Light.DarkActionBar

在 res/values/styles.xml 中定義並覆蓋關鍵顏色:

<resources>
    <style name="AppTheme" parent="android:Theme.Material">
        <!-- ActionBar 背景色 -->
        <item name="android:colorPrimary">#0000FF</item>
        <!-- 狀態欄背景色 -->
        <item name="android:colorPrimaryDark">#5500FF</item>
        <!-- 控件強調色 -->
        <item name="android:colorAccent">#FF0000</item>
    </style>
</resources>

配置後的效果:

這三個屬性的價值在於「一處定義,全局生效」——改動 colorAccent,所有複選框、進度條、滑塊等控件的強調色都會同步變化,不必逐個控件去改。

相關主題定義位於 SDK 目錄:

sdk\platforms\android-22\data\res\values\themes_material.xml

Android 5.0 的這些新特性依賴 support v7 包:

sdk\extras\android\support\v7

其中的 appcompat、cardview、gridlayout、mediarouter、palette、recyclerview 就是 5.0 引入的核心新組件。

二、讓視圖產生陰影

Material Design 用「海拔」概念表達層次,對應屬性是 elevation:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:elevation="5dp" />

三、RecyclerView

ListView 的升級版,不僅支持垂直和水平列表,還支持網格與瀑布流。通過 LayoutManager 切換:

  • LinearLayoutManager:線性布局
  • GridLayoutManager:網格布局
  • StaggeredGridLayoutManager:交錯網格(瀑布流)

此外還支持自定義 Item 動畫、自定義 Item 之間的分隔線。

四、CardView

CardView 用於實現一張立體卡片,內建圓角與陰影效果:

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:card_view="https://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:text="CardView 控件,可設置陰影與圓角效果" />
</RelativeLayout>

效果:

五、水波漣漪效果

Android 5.0 引入了 ripple drawable,為可點擊控件自帶觸摸反饋:

只需在背景中使用 ?android:attr/selectableItemBackground 或自定義 ripple 資源,就能獲得這種材質化的觸摸反饋。

六、場景轉場動畫

Material Design 的動畫強調連續性,5.0 提供了場景轉場 API:

startActivity(intent,
        ActivityOptions.makeSceneTransitionAnimation(this).toBundle());

配合在主題中開啟 android:windowContentTransitions 並指定 transition 資源,兩個頁面之間的元素就能平滑過渡。

七、如何保證向下兼容

市面上仍有大量 Android 5.0 以下的設備,要讓應用在更多機型上運行,需要從四個層面著手。

1. 資源目錄限定符

res\values\styles.xml        // 通用
res\values-v21\styles.xml    // 5.0 及以上讀取這份

布局同理:

res\layout\activity_main.xml
res\layout-v21\activity_main.xml

系統會自動根據設備版本選擇合適的資源,這是最省事的兼容方式。

2. Support Library

Android 提供 Support Library 就是為了抹平版本差異。CardView、RecyclerView 等控件都可以直接引入 v7 包,在低版本上正常使用。

3. 代碼中判斷版本

如果資源方案無法滿足需求,就只能靠代碼分支:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    // 可以使用 Material Design 特性
} else {
    // 低於 Android 5.0,走兼容邏輯
}

4. 逐步降級的設計策略

實踐中的建議是:以 Material Design 作為設計基準,然後明確哪些視覺元素在舊版本上可以省略、哪些必須用替代方案補上。陰影、漣漪、轉場動畫這些在舊版本上退化為純色背景或普通動畫通常是可以接受的;但布局結構、信息層次必須保持一致,否則用戶在不同設備上會看到兩個不同的產品。

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