Material Design : Maintaining Compatibility
Material Design : Maintaining Compatibility
不停息的脚步 发表于3年前
Material Design : Maintaining Compatibility
  • 发表于 3年前
  • 阅读 90
  • 收藏 2
  • 点赞 0
  • 评论 0

腾讯云 新注册用户 域名抢购1元起>>>   

摘要: 目前Material Design的主题仅支持Android 5.0,API 21 以上版本。本文提供了几种兼容低版本的方案。

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:

System widgets

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

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 Material Design
共有 人打赏支持
粉丝 6
博文 94
码字总数 12398
×
不停息的脚步
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: