Material Design : Maintaining Compatibility

Some material design features like the material theme and custom activity transitions are only available on Android 5.0 (API level 21) and above. However, you can design your apps to make use of these features when running on devices that support material design and still be compatible with devices running previous releases of Android.

Define Alternative Styles

You can configure your app to use the material theme on devices that support it and revert to an older theme on devices running earlier versions of Android:

  1. Define a theme that inherits from an older theme (like Holo) in res/values/styles.xml.

  2. Define a theme with the same name that inherits from the material theme in res/values-v21/styles.xml.

  3. Set this theme as your app's theme in the manifest file.

Note: If your app uses the material theme but does not provide an alternative theme in this manner, your app will not run on versions of Android earlier than 5.0.

Provide Alternative Layouts

If the layouts that you design according to the material design guidelines do not use any of the new XML attributes introduced in Android 5.0 (API level 21), they will work on previous versions of Android. Otherwise, you can provide alternative layouts. You can also provide alternative layouts to customize how your app looks on earlier versions of Android.

Create your layout files for Android 5.0 (API level 21) inside res/layout-v21/ and your alternative layout files for earlier versions of Android inside res/layout/. For example, res/layout/my_activity.xml is an alternative layout for res/layout-v21/my_activity.xml.

To avoid duplication of code, define your styles inside res/values/, modify the styles in res/values-v21/ for the new APIs, and use style inheritance, defining base styles in res/values/ and inheriting from those in res/values-v21/.

Use the Support Library

The v7 Support Libraries r21 and above includes the following material design features:

  • Material design styles for some system widgets when you apply one of the Theme.AppCompat themes.

  • Color palette theme attributes in the Theme.AppCompat themes.

  • The RecyclerView widget to display data collections.

  • The CardView widget to create cards.

  • The Palette class to extract prominent colors from images.

System widgets

The Theme.AppCompat themes provide material design styles for these widgets:

  • EditText

  • Spinner

  • CheckBox

  • RadioButton

  • SwitchCompat

  • CheckedTextView

Color Palette

To obtain material design styles and customize the color palette with the Android v7 Support Library, apply one of the Theme.AppCompat themes:

<!-- 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

The RecyclerView and CardView widgets are available in earlier versions of Android through the Android v7 Support Library with these limitations:

  • CardView falls back to a programmatic shadow implementation using additional padding.

  • CardView does not clip its children views that intersect with rounded corners.

Dependencies

To use these features in versions of Android earlier than 5.0 (API level 21), include the Android v7 Support Library in your project as a Gradle dependency:

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

The following features are available only in Android 5.0 (API level 21) and above:

  • Activity transitions

  • Touch feedback

  • Reveal animations

  • Path-based animations

  • Vector drawables

  • Drawable tinting

To preserve compatibility with earlier versions of Android, check the system version at runtime before you invoke the APIs for any of these features:

// 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
}

Note: To specify which versions of Android your app supports, use the android:minSdkVersion andandroid:targetSdkVersion attributes in your manifest file. To use the material design features in Android 5.0, set the android:targetSdkVersion attribute to 21. For more information, see the <uses-sdk> API guide.


你可能感兴趣的:(android,design,material)