Material Design系列第七篇——Maintaining Compatibility
2015-06-27 13:21
519 查看
Maintaining Compatibility
This lesson teaches you to
Define Alternative StylesProvide Alternative Layouts
Use the Support Library
Check the System Version
You should also read
Material design specificationMaterial design on Android
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:Define a theme that inherits from an older theme (like Holo) in
res/values/styles.xml.
Define a theme with the same name that inherits from the material theme in
res/values-v21/styles.xml.
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.xmlis 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.AppCompatthemes.
Color palette theme attributes in the
Theme.AppCompatthemes.
The
RecyclerViewwidget to display data collections.
The
CardViewwidget to create cards.
The
Paletteclass to extract prominent colors from images.
System widgets
TheTheme.AppCompatthemes 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 theTheme.AppCompatthemes:
<!-- extend one of the Theme.AppCompat themes --> <stylename="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
TheRecyclerViewand
CardViewwidgets are available in earlier versions of Android through the Android v7 Support Library with these limitations:
CardViewfalls back to a programmatic shadow implementation using additional padding.
CardViewdoes 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
versionat 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:minSdkVersionand
android:targetSdkVersionattributes in your manifest file. To use the material design features in Android 5.0, set the
android:targetSdkVersionattribute to
21. For more information, see the <uses-sdk> API guide.
相关文章推荐
- Dynamics AX 2012 R2 在AIF服务契约中使用DateTime
- 【Hibernate】Hibernate3.x独立运行时的Failed to load class "org.slf4j.impl.StaticLoggerBinder"错误
- LeetCode Contains Duplicate II
- hdu 1039 Easier Done Than Said?
- Rails 5 有什么新特性?
- rails简明实用入门指南
- linker command failed with exit code 1 (use -v to
- Dertermining IP information for eth0...failed - no link present check cable?
- clang: error: linker command failed with exit code 1 (use -v to see invocation)
- "perl: warning: Setting locale failed."的解决
- Task Scheduler faild to launch action "C:\Windows\SYSTEM32\cmd.exe" in instance "{xxx}" of task "\ca
- andoid-sdk 安装时出现 Stopping ADB server failed(code -1) 错
- OC基础:内存(进阶):retain.copy.assign的实现原理
- ubuntu用不了root用户:~$ su - root Password: su: Authentication failure怎么办?
- HDU 3376 Matrix Again(最大费用最大流)HDU2686加强题
- 【从翻译mos文章】Weblogic AdminServer 启动fail,报错"unable to get file lock, will retry"
- [Selenium] Explicit wait 方法
- 70.Climbing Stairs
- aidl is missing
- rails 环境搭建常用指令