使用Material Design 应用主题
自从去年Google推出Material Design这款设计语言后,新的设计规范越来越受开发者们的喜爱,这篇文章包括后续文章就是慢慢的介绍在App中使用Material Design界面主题、动画、及各种UI设计规范,至于今年推出的Design包所有design控件的介绍在以前几篇就一一介绍了。 Material Design设计语言它的设计思想为:把物理世界的体验带进屏幕,去掉现实中的杂质和随机性,保留其最原始纯净的形态、空间关系、变化与过渡,配合虚拟世界的灵活特性,还原最贴近真实的体验,达到简洁与直观的效果。
使用Material Design主题目前Material Design的Theme主要有三种:
使用Material Design主题有两种方法:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="android:Theme.Material.Light.DarkActionBar"> <!-- Customize your theme here. --> </style>
</resources>
android:theme="@android:style/Theme.Material"
这三种主题效果为: 使用Material Design主题后,各控件就有了默认的Material Design的效果了,当然我们还可以自定义Material Design 主题色调效果: <resources>
<style name="AppTheme" parent="android:Theme.Material.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="android:colorPrimary">#03A9F4</item>
<item name="android:colorPrimaryDark">#673AB7</item>
<item name="android:colorControlActivated">#03A9F4</item>
<item name="android:colorControlNormal">#E91E63</item>
<item name="android:textColorPrimary">#ffffff</item>
</style>
</resources>
其中常用到的:
当然设置状态栏的颜色也只能支持Android 5.0以上的版本。刚刚自定义主题的效果为: 一般情况下我们在使用Button时为了在点击时候使Button改变颜色,一般做法是在drawable中定义一个selector.xml,而Material Design主题中我们可以轻松在style文件中定义Button默认时候和点击时候的颜色,达到整个应用主题风格一致,如:在5.0以上设置style为: <item name="android:colorControlHighlight">#00BCD4</item>
<item name="android:colorButtonNormal">#CDDC39</item>
在5.0以下设置style为: <item name="colorControlHighlight">#00BCD4</item>
<item name="colorButtonNormal">#CDDC39</item>
效果: <style name="MyTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorControlHighlight">#00BCD4</item>
<item name="colorButtonNormal">#673AB7</item>
</style>
然后设置给这个界面的layout文件: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:theme="@style/MyTheme">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="80dp"
android:text="Button" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="80dp"
android:text="Button" />
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="80dp"
android:text="Button" />
</LinearLayout>
效果: 又如果这个界面中第一个Button的字体颜色和默认颜色需求不一样,我们还可以单独给这个Button设置一个Style主题,如: <style name="ButtonTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorControlHighlight">#00BCD4</item>
<item name="colorButtonNormal">#CDDC39</item>
<item name="android:textColor">#ffffff</item>
</style>
然后给Button设置theme: <Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="80dp"
android:theme="@style/ButtonTheme"
android:text="Button" />
效果: 综上看来,我只要对style文件利用的好,不仅可以统一整个界面主题风格,还可以减少很多代码工作和增加代码复用率,比如可以把多个Button的相同属性(默认颜色、点击颜色、字体等)抽取出来封装成一个style,在定义button时候就只需要把这个style设置给Button的android:theme属性就可以了 Material Design Theme的兼容性如果我们想要在比Android 5.0版本以下使用Material Design Theme的话: <resources>
<style name="AppTheme.Theme.AppCompat" parent="android:Theme.Material.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="android:colorPrimary">#03A9F4</item>
<item name="android:colorPrimaryDark">#673AB7</item>
<item name="android:colorControlActivated">#03A9F4</item>
<item name="android:colorControlNormal">#E91E63</item>
<item name="android:textColorPrimary">#ffffff</item>
</style>
</resources>
2、然后在res/values/目录下新建一个5.0版本以下用的styles.xml,其中该styles使用Theme.AppCompat主题,因为该主题是在support v7包下的,它兼容了Material Design的大部分风格,所以兼容必须使用Theme.AppCompat 主题,并且Activity要继承自AppCompatActivity,该styles.xml配置为: <resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#03A9F4</item>
<item name="colorPrimaryDark">#673AB7</item>
<item name="colorControlActivated">#03A9F4</item>
<item name="colorControlNormal">#E91E63</item>
</style>
</resources>
我们可以发现区别,在兼容时候需要把前面的android:前缀去掉,否则会报错,这点注意一下。。。 现在,项目就能在5.0以下的设备也使用Material Design Theme效果了,我们来看看分别运行在5.0以下版本和5.1版本上的效果吧: 在5.1以上版本: 我们可以看到,在5.0以下的版本中,虽然不能设置状态栏的颜色,但是其中大部分Material Design 的风格已经保留下来兼容的使用了,还是很炫吧。。。 Material Design布局布局说起来我就总结一下把:
2、新增Z轴,可以让控件更加具有立体感
长篇幅正文,每行建议60字符(英文)左右。短文本,建议每行30字符(英文)左右。
另外注意56dp这个数字,许多尺寸可变的控件,比如对话框、菜单等,宽度都可以按56的整数倍来设计 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |