转载自
Keegan小钢
原文链接:http://keeganlee.me/post/android/20151031
- 每个页面标题栏的标题基本会有一样的字体大小、颜色、对齐方式、内间距、外间距等,这就可以定义成样式;
- 很多按钮也都使用一致的背景、内间距、文字颜色、文字大小、文字的对齐方式等,这也可以定义成样式;
- 网络加载的进度条基本也都是一样的,同样可以定义成样式;
- 不喜欢系统的弹出框样式,那也可以自定义样式。
样式的定义
<style name="Widget.Material.Button"> <item name="background">@drawable/btn_default_material</item> <item name="textAppearance">?attr/textAppearanceButton</item> <item name="minHeight">48dip</item> <item name="minWidth">88dip</item> <item name="stateListAnimator">@anim/button_state_list_anim_material</item> <item name="focusable">true</item> <item name="clickable">true</item> <item name="gravity">center_vertical|center_horizontal</item> </style>
<!-- res/anim/button_state_list_anim_material.xml --> <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:state_enabled="true"> <set> <objectAnimator android:propertyName="translationZ" android:duration="@integer/button_pressed_animation_duration" android:valueTo="@dimen/button_pressed_z_material" android:valueType="floatType" /> <objectAnimator android:propertyName="elevation" android:duration="0" android:valueTo="@dimen/button_elevation_material" android:valueType="floatType" /> </set> </item> <!-- base state --> <item android:state_enabled="true"> <set> <objectAnimator android:propertyName="translationZ" android:duration="@integer/button_pressed_animation_duration" android:valueTo="0" android:startDelay="@integer/button_pressed_animation_delay" android:valueType="floatType"/> <objectAnimator android:propertyName="elevation" android:duration="0" android:valueTo="@dimen/button_elevation_material" android:valueType="floatType" /> </set> </item> <item> <set> <objectAnimator android:propertyName="translationZ" android:duration="0" android:valueTo="0" android:valueType="floatType"/> <objectAnimator android:propertyName="elevation" android:duration="0" android:valueTo="0" android:valueType="floatType"/> </set> </item> </selector>
<!-- res/values/styles.xml --> <resources> <style name="ButtonNormal" parent="@android:style/Widget.Material.Button" > <item name="android:background">@drawable/bg_btn_selector</item> <item name="android:textColor">@color/text_btn_selector</item> </style> </resources>
<!-- res/values/styles.xml --> <resources> <style name="ButtonNormal" parent="@android:style/Widget.Material.Button"> <item name="android:background">@drawable/bg_btn_selector</item> <item name="android:textColor">@color/text_btn_selector</item> </style> <style name="ButtonNormal.Transparent"> <item name="android:background">@drawable/bg_btn_transparent</item> <item name="android:textColor">@color/text_btn_selector</item> </style> </resources>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onAction" android:text="@string/btn_action" style="@style/ButtonNormal.Transparent" />
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
主题
- 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: 还不清楚什么用
<resources> <style name="AppTheme" parent="Theme.AppCompat"> <item name="windowActionBar">false</item> <item name="windowNoTitle">true</item> <item name="windowAnimationStyle">@style/WindowAnimation</item> </style> <!-- Standard animations for a full-screen window or activity. --> <style name="WindowAnimation" parent="@android:style/Animation.Activity"> <item name="activityOpenEnterAnimation">@anim/activity_open_enter</item> <item name="activityOpenExitAnimation">@anim/activity_open_exit</item> <item name="activityCloseEnterAnimation">@anim/activity_close_enter</item> <item name="activityCloseExitAnimation">@anim/activity_close_exit</item> </style> </resources>
<!-- res/anim/activity_close_exit.xml --> <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="false" android:zAdjustment="top"> <alpha 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" /> <translate android:fromYDelta="8%" android:toYDelta="0" android:fillEnabled="true" android:fillBefore="true" android:fillAfter="true" android:interpolator="@interpolator/decelerate_quint" android:duration="350" /> </set>
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme"> <!-- activity here --> </application>