Material Design系列第七篇——Maintaining Compatibility
Maintaining Compatibility
This lesson teaches you to
- Define Alternative Styles
- Provide Alternative Layouts
- Use the Support Library
- Check the System Version
You should also read
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.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.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
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 -->
<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
The RecyclerView and CardView widgets 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 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 and android: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.
Material Design系列第七篇——Maintaining Compatibility的更多相关文章
- Material Design系列第三篇——Using the Material Theme
Using the Material Theme This lesson teaches you to Customize the Color Palette Customize the Status ...
- Material Design系列第八篇——Creating Lists and Cards
Creating Lists and Cards //创建列表和卡片 To create complex lists and cards with material design styles in ...
- Material Design系列第六篇——Defining Custom Animations
Defining Custom Animations //自定义动画 This lesson teaches you to //本节课知识点 Customize Touch Feedback //1. ...
- Material Design系列第五篇——Working with Drawables
Working with Drawables This lesson teaches you to Tint Drawable Resources Extract Prominent Colors f ...
- Material Design系列第四篇——Defining Shadows and Clipping Views
Defining Shadows and Clipping Views This lesson teaches you to Assign Elevation to Your Views Custom ...
- Android Material Design系列之主题样式介绍说明
今天这篇文章应该算是Material Design系列的补充篇,因为这篇文章本来应该放到前面讲的,因为讲的是主题嘛,对于一些状态和颜色的介绍,因为我们一新建一个项目时,系统自带了三个属性的颜色,现在就 ...
- Material Design系列第二篇——Getting Started
Getting Started This lesson teaches you to Apply the Material Theme Design Your Layouts Specify Elev ...
- Material Design系列第一篇——Creating Apps with Material Design
Creating Apps with Material Design //创建Material Design的App Material design is a comprehensive guide ...
- Android Material Design 系列之 SnackBar详解
SnackBar是google Material Design提供的一种轻量级反馈组件.支持从布局的底部显示一个简洁的提示信息,支持手动滑动取消操作,同时在同一个时间内只能显示一个SnackBar. ...
随机推荐
- doctest初次体验
测试代码放在两个地方才有效果,一个是模块开头,一个是函数声明语句的下一行 doctest 的概念模型 在python的官方文档中,对doctest是这样介绍的: doctest模块会搜索那些看起来像是 ...
- 详细的Log4j使用教程
日志是应用软件中不可缺少的部分,Apache的开源项目log4j是一个功能强大的日志组件,提供方便的日志记录.在apache网站:jakarta.apache.org/log4j 可以免费下载到Log ...
- unity-------------UI的界面调节
Rect Transform 我们都知道,Unity3D中所有的GameObject都必须要携带一个Transform组件,且该组件无法移除,那么作为UI显示的GameObject则不是携带Trans ...
- [ACM] POJ 1611 The Suspects (并查集,输出第i个人所在集合的总人数)
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 21586 Accepted: 10456 De ...
- 常用RAID简介_001
RAID定义 RAID(Redundant Array of Independent Disk 独立冗余磁盘阵列)技术是加州大学伯克利分校1987年提出,最初是为了组合小的廉价磁盘来代替大的昂贵磁盘, ...
- CentOS 7 打开关闭FirewallD防火墙端口命令
CentOS 7 使用firewalld代替了原来的iptables,使用方法如下: >>>关闭防火墙 systemctl stop firewalld.service ...
- git fetch , git pull
要讲清楚git fetch,git pull,必须要附加讲清楚git remote,git merge .远程repo, branch . commit-id 以及 FETCH_HEAD. 1. [g ...
- iOS7以上: 实现如“日历”的 NavigationBar
第一步,隐藏导航栏底部的分割线 如何隐藏导航栏底部的分割线(shadow image/ hairline)? navigationBar.clipsToBounds = YES; //隐藏 navig ...
- Java华氏转摄氏
package test; import java.util.Scanner; public class temperature { public static void main(String[] ...
- Objective-C语法之nonatomic和atomic之间的区别
atomic: 保证 setter/getter 这两个方法的一个原语操作.如果有多个线程同时调用 setter 的话,不会出现某一个线程执行 setter 全部语句之前,另一个线程开始执行 set ...