本篇提到的方法帖汞,是通過更改應(yīng)用應(yīng)用主題中的style的屬性抠蚣,來使得自定義布局可以應(yīng)用到全局。
更改theme默認(rèn)顏色
Theme是應(yīng)用到整個application或activity的樣式傲茄,其實也是一種style坎缭,只不過比較龐大。使用api 25新建module尚胞,默認(rèn)使用繼承與“Theme.AppCompat.Light.DarkActionBar”的“AppTheme”主題硬霍,并更改了三個顏色屬性。AppTheme定義在module的values/styles.xml中笼裳。更改這三個顏色唯卖,發(fā)現(xiàn)分別對應(yīng)app的標(biāo)題欄、手機(jī)頂欄和組件默認(rèn)顏色躬柬。將三者分別改為紅拜轨、綠、藍(lán)三色允青,以下是代碼及對應(yīng)的界面:
事實上撩轰,這三個屬性是在最底層的Theme樣式中定義的,因此可以應(yīng)用到全部組件昧廷。
自定義全局組件
既然可以通過更改colorAccent來改變所有組件的顏色,那么肯定可以通過類似的方式偎箫,定義組件樣式木柬,并在全局使用。下面來自定義一個全局button淹办∶颊恚可以想到,應(yīng)該有一個屬性,是用來指定button樣式的速挑,只要重為這個屬性賦值即可谤牡。事實上,這些屬性都是屬于android.R.attr的姥宝,具體的說明可見官方文檔R.attr翅萤。而自定義button,需要使用buttonStyle屬性腊满。
默認(rèn)Button樣式
buttonStyle的具體樣式也是在Theme中指定的:<item name="buttonStyle">@style/Widget.Button</item>
套么。順便說下Theme樣式的定義可以在C:\Users\【username】\AppData\Local\Android\sdk\platforms\android-xx\data\res\values
中找到。Widget.Button定義如下:
<style name="Widget.Button">
<item name="background">@drawable/btn_default</item>
<item name="focusable">true</item>
<item name="clickable">true</item>
<item name="textAppearance">?attr/textAppearanceSmallInverse</item>
<item name="textColor">@color/primary_text_light</item>
<item name="gravity">center_vertical|center_horizontal</item>
</style>
其中@drawable/btn_default的定義如下:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:state_enabled="true"
android:drawable="@drawable/btn_default_normal" />
<item android:state_window_focused="false" android:state_enabled="false"
android:drawable="@drawable/btn_default_normal_disable" />
<item android:state_pressed="true"
android:drawable="@drawable/btn_default_pressed" />
<item android:state_focused="true" android:state_enabled="true"
android:drawable="@drawable/btn_default_selected" />
<item android:state_enabled="true"
android:drawable="@drawable/btn_default_normal" />
<item android:state_focused="true"
android:drawable="@drawable/btn_default_normal_disable_focused" />
<item
android:drawable="@drawable/btn_default_normal_disable" />
</selector>
這之中的drawable文件已經(jīng)無法右鍵go to declaration了碳蛋,所以是圖片文件胚泌。可在C:\Users\【username】\AppData\Local\Android\sdk\platforms\android-xx\data\res\drawable-mdpi
中找到對應(yīng)的圖片肃弟。默認(rèn)樣式如下:
自定義Button樣式
在module的style.xml中自定義樣式玷室,并用其重定義buttonStyle屬性:
<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="buttonStyle">@style/MyButtonStyle</item>
</style>
<style name="MyButtonStyle" parent="@android:style/Widget.Button">
<item name="android:background">#0000ff</item>
</style>
由于默認(rèn)樣式是Widget.Button,因此這里直接繼承它笤受,然后更改希望更改的屬性穷缤。這樣不想更改的屬性就不需要再自己定義一遍。這里把background屬性重定義成藍(lán)色感论。然后在應(yīng)用的AppTheme樣式中绅项,將buttonStyle屬性指定為自定義的MyButtonStyle樣式。注意比肄,使用系統(tǒng)的style或?qū)傩钥旃ⅲQ前要使用"android:"。完成后布局中button的樣式如下:
以這種方式芳绩,類似地掀亥,可以將所有組件更改為自定義樣式。而且就buttonStyle來說妥色,我們已經(jīng)知道此屬性定義在Theme這個style中搪花,因此在所有基于Theme的樣式中,都可以使用這種方法嘹害,而不擔(dān)心沒有對應(yīng)的屬性撮竿。