先介紹下修改原理:首先打開位於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