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.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:
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:
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
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. ...
随机推荐
- 获取Java正在执行的方法
new Object(){}.getClass().getEnclosingMethod().getName(); Thread.currentThread().getStackTrace()
- Python——eventlet.event
该模块提供eventlet的事件支持,事件提供了跨 greenthread 的操作原语. 同一个事件对象既可以发出事件也可以接收(等待)事件,不同的协程共享这一个事件对象,就为不同协程之间基于事件的同 ...
- Hadoop分布式集群部署(单namenode节点)
Hadoop分布式集群部署 系统系统环境: OS: CentOS 6.8 内存:2G CPU:1核 Software:jdk-8u151-linux-x64.rpm hadoop-2.7.4.tar. ...
- cocos2d - 翻转两个Sprite
用两个图片初始化两个CCSprite,一样的大小,重叠在一起,分别叫做 foregroundNode 和 backgroundNode . - (void)flipover { if (isFlipi ...
- Java面试经典题:线程池专题
1.什么是线程池 线程池的基本思想是一种对象池,在程序启动时就开辟一块内存空间,里面存放了众多(未死亡)的线程,池中线程执行调度由池管理器来处理.当有线程任务时,从池中取一个,执行完成后线程对象归池, ...
- mysql触发器应用和创建表错误代码: 1118 Row size too large. 解决
1.针对数据库查询问题的方便,可以建立重要表的log备份记录表,在主表的添加,修改,删除添加触发器,修改触发器增加触发字段的点,限制条件. 数据库log表查问题比从线上多台服务器上下载日志文件相对方便 ...
- MTK 锁屏配置
常常我们开 发程序的时候我们不需要系统唤醒系统锁屏功能,用户有时候在看电视或视频的时候不希望系统的锁屏 功能启动,既不想锁频,然而系统却在我们看电视或者视频的时候出来个锁屏的界面进行锁频拉,我们还要想 ...
- c++String类的运算符重载---21
原创博文,转载请标明出处--周学伟http://www.cnblogs.com/zxouxuewei/ 一,创建测试程序包 测试代码如下: /* Date: 2017-5-4 * Descripti ...
- jrMz and angles(水题)
jrMz and angles Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)T ...
- hdu5289 2015多校联合第一场1002 Assignment
题意:给出一个数列.问当中存在多少连续子区间,当中子区间的(最大值-最小值)<k 思路:设dp[i]为从区间1到i满足题意条件的解.终于解即为dp[n]. 此外 如果对于arr[i] 往左遍历 ...