样式的定义

Android的样式一般定义在res/values/styles.xml文件中,其中有一个根元素,而具体的每种样式定义则是通过下的子标签

@drawable/btn_default_material

?attr/textAppearanceButton

48dip

88dip

@anim/button_state_list_anim_material

true

true

center_vertical|center_horizontal

其中,stateListAnimator指定状态改变时的动画,button_state_list_anim_material的代码如下:

xmlns:android="http://schemas.android.com/apk/res/android">

android:propertyName="translationZ"

android:duration="@integer/button_pressed_animation_duration"

android:valueTo="@dimen/button_pressed_z_material"

android:valueType="floatType" />

android:propertyName="elevation"

android:duration="0"

android:valueTo="@dimen/button_elevation_material"

android:valueType="floatType" />

android:state_enabled="true">

android:propertyName="translationZ"

android:duration="@integer/button_pressed_animation_duration"

android:valueTo="0"

android:startDelay="@integer/button_pressed_animation_delay"

android:valueType="floatType"/>

android:propertyName="elevation"

android:duration="0"

android:valueTo="@dimen/button_elevation_material"

android:valueType="floatType" />

android:propertyName="translationZ"

android:duration="0"

android:valueTo="0"

android:valueType="floatType"/>

android:propertyName="elevation"

android:duration="0"

android:valueTo="0"

android:valueType="floatType"/>

可以看到,每种状态的动画为属性动画集,属性动画的用法请参考Property Animation篇。现在我想继承Widget.Material.Button样式,改变背景和文字颜色,那么,代码如下:

@drawable/bg_btn_selector

@color/text_btn_selector

其中,@drawable/bg_btn_selector和@color/text_btn_selector的实现请参照selector篇。有些按钮,我只想改变文字颜色,但背景想让它透明,这时就可以用点前缀的方式继承以上的样式,代码如下:

@drawable/bg_btn_selector

@color/text_btn_selector

@drawable/bg_btn_transparent

@color/text_btn_selector

引用的时候只要在相应的Button里添加style就可以了,代码如下:

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:onClick="onAction"

android:text="@string/btn_action"

style="@style/ButtonNormal.Transparent" />

有时候,定义的样式太多,如果都放在styles.xml文件里,那这文件也太臃肿了。因此,可以将样式分类拆分成多个文件。Android系统本身也拆分为多个文件存放的,如下列表全都是样式文件:

styles.xml

styles_device_defaults.xml

styles_holo.xml

styles_leanback.xml

styles_material.xml

styles_micro.xml

themes.xml

themes_device_defaults.xml

themes_holo.xml

themes_leanback.xml

themes_material.xml

themes_micro.xml

其中,主要分为两大类,styles定义了简单的样式,而themes则定义了主题。

主题

以上的简单例子只用于单个View,这是样式最简单的用法。但样式的用法不只是用于单个View,也能用于Activity或整个Application,这时候需要在相应的标签或标签里设置android:theme属性,引用的其实也是style**,但一般称为主题。

Android系统提供了多套主题,查看Android的frameworks/base/core/res/res/values目录,就会看到有以下几个文件(目前为止):

themes.xml: 低版本的主题,目标API level一般为10或以下

themes_holo.xml: 从API level 11添加的主题

themes_device_defaults.xml: 从API level 14添加的主题

themes_material.xml: 从API level 21添加的主题

themes_micro.xml: 应该是用于Android Wear的主题

themes_leanback.xml: 还不清楚什么用

不过在实际应用中,因为大部分都采用兼容包的,一般都会采用兼容包提供的一套主题:Theme.AppCompat。AppCompat主题默认会根据不同版本的系统自动匹配相应的主题,比如在Android 5.0系统,它会继承Material主题。不过这也会导致一个问题,不同版本的系统使用不同主题,就会出现不同的体验。因此,为了统一用户体验,最好还是自定义主题。

自定义主题也很简单,只要继承某一父主题,然后在标签或中引用就可以了。主题的定义示例如下:

false

true

@style/WindowAnimation

@anim/activity_open_enter

@anim/activity_open_exit

@anim/activity_close_enter

@anim/activity_close_exit

其中,WindowAnimation重新指定了Activity的转场动画,以下为activity_close_exit的示例代码:

xmlns:android="http://schemas.android.com/apk/res/android"

android:shareInterpolator="false"

android:zAdjustment="top">

android:fromAlpha="0.0"

android:toAlpha="1.0"

android:interpolator="@interpolator/decelerate_quart"

android:fillEnabled="true"

android:fillBefore="false"

android:fillAfter="true"

android:duration="200" />

android:fromYDelta="8%"

android:toYDelta="0"

android:fillEnabled="true"

android:fillBefore="true"

android:fillAfter="true"

android:interpolator="@interpolator/decelerate_quint"

android:duration="350" />

这是比较简单的视图动画,视图动画具体用法可参考View Animation篇。接着,若要使用到整个Application,则在AndroidManifest.xml的标签设置android:theme属性,示例代码如下:

android:allowBackup="true"

android:icon="@mipmap/ic_launcher"

android:label="@string/app_name"

android:theme="@style/AppTheme">

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐