话说,一般情况下需要定义按钮的样式属性时只需要在需要的按钮xml定义处手工设置样式属性就行了。但是如果出现一个应用中有好多按钮,并且这些按钮都需要一样的属性,逐个的去设置style属性就不应该了。在这种情况下就可以在主题中定义一个用于所有按钮的样式。
因为要修改应用的主题,所以第一步查找buttonStyle属性。在res/values/styles.xml文件中,按住ctrl键点击Theme.AppCompat.Light.DarkActionBar,在Android studio里追溯主题属性。
定位第一次:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 定位第二次:
<style name="Theme.AppCompat.Light.DarkActionBar" parent="Base.Theme.AppCompat.Light.DarkActionBar"/>定位第三次:
<style name="Base.Theme.AppCompat.Light.DarkActionBar" parent="Base.Theme.AppCompat.Light"> <item name="actionBarPopupTheme">@style/ThemeOverlay.AppCompat.Light</item> <item name="actionBarWidgetTheme">@null</item> <item name="actionBarTheme">@style/ThemeOverlay.AppCompat.Dark.ActionBar</item> <!-- Panel attributes --> <item name="listChoiceBackgroundIndicator">@drawable/abc_list_selector_holo_dark</item> ...
定位第四次:
<style name="Base.Theme.AppCompat.Light" parent="Base.V21.Theme.AppCompat.Light"/>定位第五次:
<style name="Base.V21.Theme.AppCompat.Light" parent="Base.V7.Theme.AppCompat.Light"> <!-- Action Bar styling attributes --> <item name="actionBarSize">?android:attr/actionBarSize</item> <item name="actionBarDivider">?android:attr/actionBarDivider</item> <item name="actionBarItemBackground">@drawable/abc_action_bar_item_background_material</item> <item name="actionButtonStyle">?android:attr/actionButtonStyle</item> ...
定位第六次:
<style name="Base.V7.Theme.AppCompat.Light" parent="Platform.AppCompat.Light"> <item name="windowNoTitle">false</item> <item name="windowActionBar">true</item> <item name="windowActionBarOverlay">false</item> ...
定位第七次:
<style name="Platform.AppCompat.Light" parent="Platform.V11.AppCompat.Light"/>
定位第八次:
<style name="Platform.V11.AppCompat.Light" parent="android:Theme.Holo.Light"> <item name="android:windowNoTitle">true</item> <item name="android:windowActionBar">false</item> ...
定位第九次:
<style name="Theme.Holo.Light" parent="Theme.Light"> <item name="colorForeground">@color/bright_foreground_holo_light</item> <item name="colorForegroundInverse">@color/bright_foreground_inverse_holo_light</item> <item name="colorBackground">@color/background_holo_light</item> ...
一直定位到上面这个大文件,在这里面找到了buttonStyle属性,如下面这条语句所示:
<!-- Button styles --> <item name="buttonStyle">@style/Widget.Holo.Light.Button</item> ...
第二步,在res/values/styles.xml文件中自定义样式,如下所示,新建样式名为ButtonTheme,该样式仅定义了android:background属性,属性值为定义在drawable目录的xml文件。指定
ButtonTheme样式的父样式为android:Widget.Holo.Light.Button,确保所有按钮都继承常规按钮的属性。
//自定义按钮样式 <style name="ButtonTheme" parent="android:style/Widget.Holo.Light.Button"> <item name="android:background">@drawable/button_shape</item> </style>
第三步在系统主题的style标签下,将第二步中自定义的按钮样式添加进去,如下所示:
<!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> //状态栏 <item name="colorPrimary">@color/colorPrimary</item> //工具栏 <item name="colorPrimaryDark">@color/colorPrimaryDark</item> //主要用于给EditText这样的组件着色 <item name="colorAccent">@color/colorAccent</item> //自定义按钮主题 <item name="android:buttonStyle">@style/ButtonTheme</item> </style> 这样应用中的所有按钮的样式就都可以使用自定义的主题。如果在个别按钮处单独指定样式,那么按钮的样式在单独指定按钮处以单独指定的为准。 例子中的button_shape.xml文件的内容为:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/button_pressed" android:state_pressed="true"/> <item android:drawable="@drawable/button_normal"/> </selector>
其中button_normal.xml文件的内容是:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <solid android:color="#BDBDBD" /> <corners android:radius="10dp" /> <padding android:bottom="5dp" android:left="10dp" android:right="10dp" android:top="5dp" /> </shape>
其中button_presssed.xml文件的内容为:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <solid android:color="@color/colorAccent" /> <corners android:radius="10dp" /> <padding android:bottom="5dp" android:left="10dp" android:right="10dp" android:top="5dp" /> </shape>
所以在整个应用中的按钮的样式都会按照文件中定义的样式生效。效果图为:左图是按钮的正常显示效果图,右图是按下按钮的瞬间效果图。
应用中的其他按钮的效果都和上图中一样,这样也不需要在每个按钮的xml定义处都一一指定,更加方便。