天天看點

Android(Lollipop/5.0) Material Design(八) 保持相容性

Material Design系列

Android(Lollipop/5.0) Material Design(一) 簡介

Android(Lollipop/5.0) Material Design(二) 入門指南

Android(Lollipop/5.0) Material Design(三) 使用Material主題

Android(Lollipop/5.0) Material Design(四) 建立清單和卡片

Android(Lollipop/5.0) Material Design(五) 定義陰影和裁剪View

Android(Lollipop/5.0) Material Design(六) 使用圖檔

Android(Lollipop/5.0) Material Design(七) 自定義動畫

Android(Lollipop/5.0) Material Design(八) 保持相容性

官網: https://developer.android.com/training/material/compatibility.html

Define Alternative Styles  定義替代樣式

讓你的app,使用Material Design的主題運作在支援它的裝置上,并在早期版本的裝置上可以運作較早的主題: 1. 在res/values/styles.xml 定義一個主題繼承較早的主題 2. 在res/values-v21/styles.xml 定義一個相同名字的繼承自Material主題 的主題  3. 在manifest中應用定義的主題 注:如果你的app使用了Material 主題,而不提供較早的主題,那麼将不能運作在早期版本的裝置上

Provide Alternative Layouts  提供替代布局

如果你設計的layout不引用任何的5.0中的xml屬性,那麼可以運作在早期版本的Android裝置上。否則,你可提供一個替代布局。 替代布局建立在res/layout-v21/ 為了避免重複代碼,可以在res/values/  定義你的styles,新風格的在res/values-21/ 中定義,并使用style的繼承,在res/values中定義一個baseStyle,在res/values-21中繼承它。

Use the Support Library  使用支援庫

v7 support library 包括以下的一些特性: · 在應用了一個Theme.AppCompat 主題後,系統的一些元件就有了Material Design 的風格 · 在Theme.AppCompat 主題中,有調色主題 · RecyclerView 元件顯示資料集 · CardView 元件建立卡片 · 從圖像中取色

System widgets  系統元件

Theme.AppCompat 主題提供的Material Design 風格的元件有:

· EditText · Spinner · CheckBox · Radiobutton · SwitchCompat · CheckedTextView

Color Palette

使用v7支援庫,獲得Material Design 風格定義顔色闆,應用一個Theme.AppCompat 主題:

<!-- extend one of the Theme.AppCompat themes -->
<style name="Theme.MyTheme" parent="Theme.AppCompat.Light">
    <!-- customize the color palette -->
    <item name="colorPrimary">@color/material_blue_500</item>
    <item name="colorPrimaryDark">@color/material_blue_700</item>
    <item name="colorAccent">@color/material_green_A200</item>
</style>      

Lists and Cards

使用v7支援庫後,在早期的Android版本上也可運作。

Dependencies

gradle 依賴:

dependencies {
    compile 'com.android.support:appcompat-v7:21.0.+'
    compile 'com.android.support:cardview-v7:21.0.+'
    compile 'com.android.support:recyclerview-v7:21.0.+'
}      

Check the System Version  檢查系統版本

以下特性隻能在Android 5.0(API級别21)及以上:

· Activity transitions  活動轉換

· Touch feedback    觸覺回報

· Reveal animations  顯示動畫

· Path-based animations  基于路徑動畫

· Vector drawables  矢量圖檔

· Drawable tinting  圖檔染色

檢查代碼:

// Check if we're running on Android 5.0 or higher
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    // Call some material design APIs here
} else {
    // Implement this feature without material design
}      

注:要讓app支援5.0,需要在manifest中Android:targetSdkVersion=21。