Android Material Design-Maintaining Compatibility(保持兼容性)-(七)



转载请注明出处:http://blog.csdn.net/bbld_/article/details/40634829

翻译自: http://developer.android.com/training/material/compatibility.html


一些materialdesign中的功能像material主题和自定义activity的过渡只能在Android 5.0(API级别21)或以上的系统版本中才能使用。但是你可以设计你的app去使用那些功能,无论在支持materialdesign的设备上亦或是早期的Android版本上。

 

定义可选的样式

你可以配置你的app去使用material design在支持它的设备上,在早期的Android版本中使用旧的主题:

1.        定义一个主题,它继承一个旧的主题(像Holo),放在res/values/styles.xml。

2.        定义一个相同名字主题,继承material主题,放在res/values-21/styles.xml。

3.        在manifest文件中设置这个主题为你的app主题。

注意:如果你的app使用了material主题但是没有提供一个可替代的主题,你的app将无法在Android 5.0之前的系统中运行。

 

提供可替代的布局

如果你根据设计准则不使用任何Android5.0引入的新的XML的属性去设计你的布局,他们能在Android的早期版本上运行。否则,你可以提供可选择的布局。你也可以提供可替代的布局,以自定义你的app看起来是在早期的Android版本上。

为Android 5.0(API级别21)或以上系统创建布局时则布局文件放在res/layout-v21/文件夹里,早期Android版本的可替代的布局则放在res/layout/文件夹里。例如,res/layout/my_activity.xml是res/layout-v21/my_activity.xml的一个可替代的布局

为了避免重复的代码,在res/values/里定义你的样式资源,为新的API修改的样式则放在res/values-v21/文件夹里,并且使用样式继承,在res/values/中定义基本的样式,在res/values-v21/.中继承基本的样式。

 

使用支持库

v7支持库r21及以上的版本包含以下的material design的特点:

l  当你应用Theme.AppCompat的主题时,一些控件具有materialdesign style的特征。

l  Theme.AppCompat具有Color palette theme属性。

l  RecyclerView控件显示数据集合。

l  CardView控件创建卡片。

l  Palette类去从图片中提取突出的颜色。

 

系统控件

Theme.AppCompat主题为这些控件提供了material design styles:

l  EditText

 Spinner

 CheckBox

l  RadioButton

 SwitchCompat

 CheckedTextView

 

颜色调色板

为了在Android v7支持库中获得material design styles和自定义调色板,使用一个Theme.AppCompat的主题:

  1. <!-- extend one of the Theme.AppCompat themes -->  
  2. <style name="Theme.MyTheme" parent="Theme.AppCompat.Light">  
  3.     <!-- customize the color palette -->  
  4.     <item name="colorPrimary">@color/material_blue_500</item>  
  5.     <item name="colorPrimaryDark">@color/material_blue_700</item>  
  6.     <item name="colorAccent">@color/material_green_A200</item>  
  7. </style>  
<!-- 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>


列表和卡片

RecyclerViewCardView控件可以通过Android v7支持包在早期的Android版本上使用。但是有这些限制:

l  CardView回退到使用有规则的阴影通过使用额外的填充。

l  CardView不会裁剪它的子视图,使用圆角相交。


依赖

要在早于Android5.0(API级别21)的系统中使用这些功能,需添加Android v7支持库到你的项目中,下面是添加Gradle依赖

  1. dependencies {  
  2.     compile 'com.android.support:appcompat-v7:21.0.+'  
  3.     compile 'com.android.support:cardview-v7:21.0.+'  
  4.     compile 'com.android.support:recyclerview-v7:21.0.+'  
  5. }  
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.+'
}

补充:Eclipse中添加依赖在前面的博客中我已做了说明(Android Material Design-Creating Lists and Cards(创建列表和卡片)-(三))。


检查系统版本

以下功能仅适用于Android的5.0(API等级21)以上:

l  Activity transitions(Activity的过渡转换)

l  Touch feedback(触摸反馈)

l  Reveal animations(显示、揭露动画)

l  Path-based animations(基于路径的动画)

l  Vector drawables(矢量绘图资源)

l  Drawable tinting(Drawable着色)

为了保持与早期Android版本的兼容性,在调用这些API之前检查系统的版本

 

  1. // Check if we're running on Android 5.0 or higher  
  2. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {  
  3.     // Call some material design APIs here  
  4. else {  
  5.     // Implement this feature without material design  
  6. }  
// 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
}

 注意:要指定你的app所能支持的版本,使用在你的manifest文件中android:minSdkVersion和android:targetSdkVersion属性去声明。要在Android 5.0中需要使用material desing的功能,设置android:targetSdkVersionattribute属性的值为21.。更多的信息请参阅<uses-sdk>的API文档说明。

 

 

-----------------------------------------------------------------  Material Design系列的翻译完结  ----------------------------------------------------------------------

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值