Android 5.0以下使用Material Design 特性

来源:互联网 发布:psp go 自制软件 编辑:程序博客网 时间:2024/06/08 03:24

最近在学习Material Design的一些新特性,于是呢就用博客来记录一下自己的学习过程吧,正好也可以跟有缘人分享一下大笑

Google提供了 v7 Support Libraries 让我们可以在 API level 21以下的设备上使用一些Material Design的特性。

但是遗憾的以下的这些特性还是只能在5.0+使用:

    • Activity transitions
    • Touch feedback
    • Reveal animations
    • Path-based animations
    • Vector drawables
    • Drawable tinting
是不是很坑爹…… 没办法啦

通过引用v7包,我们可以继承Theme.AppCompat主题来使用部分有Materail Design特性的控件:

    • EditText
    • Spinner
    • CheckBox
    • RadioButton
    • SwitchCompat
    • CheckedTextView
并且使用v7包我们还可以通过继承Theme.AppCompat来定制Color Palette

<!-- 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>不但如此,我们还可以使用RecyclerView和CardView,怎么样 还是挺不错的吧。不过记得在引用这两个控件的时候要记得去添加依赖库哦~~</span></span><pre name="code" class="html">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.+'}<span style="font-family:Roboto, sans-serif;">那么接下来问题来了?(此处省略15个字)</span>
那如果你说如果我的Target版本就5.0+的呢?那是不是就要考虑向下兼容的问题了。
首先我们可以定义可供选择的样式(styles文件)
1、在一个继承至老版本主题(像Holo主题)位于res/values/styles.xml的样式文件中定义正常的样式,而在res/values-v21/styles.xml中继承至Material Design Theme 的styls.xml中定义包含Material Design 特性的样式。
当然在有了styles.xml文件之后我们肯定要有可供选择的layout文件咯
2、在 res/layout-v21/中定义你专门为Android5.0+提供的布局,而在 res/layout/中定义给5.0一下版本定义的布局。

(有个秘密要偷偷告诉你们O(∩_∩)O:在 res/values/中定义基本的样式,然后在 res/values-v21/中继承至刚才定义的样式在添加上属于Material Design的属性。这样就可以避免代码的重复~~~)

当然如果你要在Java代码中动态调用Material Design的API的话,就需要对当前的系统版本进行检测然后再调用




0 0
原创粉丝点击