先介绍下修改原理:首先打开位于android.widget包下面的Button.java文件,这里有一句关键的代码以下:java
public Button(Context context, AttributeSet attrs) { this(context, attrs, com.android.internal.R.attr.buttonStyle); }
其中com.android.internal.R.attr.buttonStyle就是咱们修改样式的关键了,网上的教程的修改方法大都是:android
<Button style="@style/ButtonStyle" android:layout_width="wrap_content" android:layout_height="40dp" android:layout_weight="1" android:text="价格" />
也就是在对应的xml里面button控件里面编写style达到目的。
可是若是咱们的app须要彻底统一整个应用的button的样式,那么就须要在每个button里面添加style。
这显然效率过低下了。web
接下来打开咱们项目中values文件夹下面的styles.xml文件,咱们建立安卓项目的时候,会有一个默认的styles文件。
打开以后找到这段代码:app
<style name="AppBaseTheme" parent="Theme.Holo.Light"> <!-- Theme customizations available in newer API levels can go in res/values-vXX/styles.xml, while customizations related to backward-compatibility can go here. --> </style> <!-- Application theme. --> <style name="AppTheme" parent="AppBaseTheme">
不保证读者的默认styles.xml和个人是同样的,不过大概是这个样子,有可能读者的最低支持是2.三、那么就没有Them.Light。
咱们使用eclipse的快捷键打开这个Theme.Holo.Light。能够看到以下代码:less
<style name="Theme.Holo.Light" parent="Theme.Light"> <item name="colorForeground">@android:color/bright_foreground_holo_light</item> <item name="colorForegroundInverse">@android:color/bright_foreground_inverse_holo_light</item> <item name="colorBackground">@android:color/background_holo_light</item> <item name="colorBackgroundCacheHint">@android:drawable/background_cache_hint_selector_holo_light</item> <item name="disabledAlpha">0.5</item> <item name="backgroundDimAmount">0.6</item> <!--此处省略大部分中间样式--> <!-- Button styles --> <item name="buttonStyle">@android:style/Widget.Holo.Light.Button</item> <item name="buttonStyleSmall">@android:style/Widget.Holo.Light.Button.Small</item> <item name="buttonStyleInset">@android:style/Widget.Holo.Light.Button.Inset</item> <item name="buttonStyleToggle">@android:style/Widget.Holo.Light.Button.Toggle</item> <item name="switchStyle">@android:style/Widget.Holo.Light.CompoundButton.Switch</item> <item name="selectableItemBackground">@android:drawable/item_background_holo_light</item> <item name="borderlessButtonStyle">@android:style/Widget.Holo.Light.Button.Borderless</item> <item name="homeAsUpIndicator">@android:drawable/ic_ab_back_holo_light</item>
从上面的代码,能够看到buttonStyle这个样式:
这个就是咱们修改的关键了,若是读者有兴趣查看Holo主题的button样式是怎么编写的,能够自行查看,这里不是介绍的重点。
接下来开始定义咱们本身的全局button的样式。eclipse
<resources> <!-- 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> <item name="colorAccent">@color/colorAccent</item> <item name="android:buttonStyle">@style/ButtonStyle</item> </style> <style name="ButtonStyle" parent="@android:style/Widget.Button"> <item name="android:background">@drawable/comm_button_style</item> <item name="android:textColor">@android:color/holo_green_dark</item> </style> </resources>
咱们在AppTheme 里面添加一个item,名字叫作android:buttonStyle,而后在下面编写咱们要修改的butto的样式。
这里有一点须要注意的就是咱们须要继承android:style/Widget.Button这个样式,由于若是不继承的话,咱们就须要修改全部button的属性。而当前的示例中,我修改的只是background,其它属性咱们照旧搬安卓本地主题的设置。
并且平时咱们在编写界面的时候,对button设置了background以后,其实只是覆盖了系统默认button的其中一个样式而已,这点咱们从button的源码能够看获得。
若是你不继承Widget.Button的话,那么出来的效果多是面目全非的。this
修改结果:spa
这种修改方式能够推广到其它的控件的修改,至于修改思路,能够参照上面介绍的button样式的修改方法。code