簡介
當我們新建一個項目的時候,都會給Application或者某些Activity設置theme:
<android:theme="@style/AppTheme">
這時就需要我們選擇或者自定義一個主題,那每個屬性又都是什麼含義呢?
來看個例子:
<style name="AppTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">
<!--主色調 -->
<item name="colorPrimary">@color/colorPrimary</item>
<!--狀態欄顏色-->
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<!--控制各個控件被選中時的顏色-->
<item name="colorAccent">@color/colorAccent</item>
<!--頁面背景色-->
<item name="android:windowBackground">@color/colorWindowBg</item>
<!--底部導航欄顏色-->
<item name="android:navigationBarColor">@color/colorPrimaryDark</item>
<!--ToolBar上的Title顏色-->
<item name="android:textColorPrimary">@color/colorText</item>
<!--各個控制控件的默認顏色 與colorAccent相反-->
<item name="android:colorControlNormal">@color/colorPrimary</item>
<!--控制所有可點擊的View的漣漪效果顏色-->
<item name="android:colorControlHighlight">@color/colorCheck</item>
<!--控制normal狀態下button的顏色-->
<item name="android:colorButtonNormal">@color/colorPrimary</item>
<!--EditText 輸入框中字體的顏色-->
<item name="editTextColor">@color/colorText</item>
</style>
相關屬性說明
- 1.colorPrimary: Toolbar導航欄的底色。
- 2.colorPrimaryDark:狀態欄的底色,注意這裏只支持Android5.0以上的手機。
- 3.textColorPrimary:整個當前Activity的字體的默認顏色。
- 4.android:windowBackground:當前Activity的窗體顏色。
- 5.colorAccent:CheckBox,RadioButton,SwitchCompat等控件的點擊選中顏色
- 6.colorControlNormal:CheckBox,RadioButton,SwitchCompat等默認狀態的顏色。
- 7.colorButtonNormal:默認狀態下Button按鈕的顏色。
- 8.editTextColor:默認EditView輸入框字體的顏色。
特別介紹
下面我們要說的是其他一些和顏色相關的屬性,這些屬性可能你平時並不常用,但合理運用它們不僅可以使你的界面更加絢麗而且可以達到事半功倍的效果。(再次聲明以下屬性適用於5.0及其以上系統)
1、colorControlHighlight
colorControlHighlight用於控制所有可點擊的View的漣漪效果顏色,以button舉例來說
普通狀態下的button
現在我們在AppTheme中添加下面這條屬性
<item name="android:colorControlHighlight">@color/colorCheck</item>
2、colorButtonNormal
用於控制normal狀態下button的顏色(注意和button的background屬性的區別,設置colorButtonNormal後button依舊會具有漣漪效果,而設置background就沒有漣漪效果了)
設置colorButtonNormal後button的normal狀態如下
3、android:navigationBarColor
控制底部導航欄的背景色(注意前邊必須有android:),例如:
常用控件顏色屬性
EditText
- unfocused —— colorControlNormal
- focus —— colorAccent
- cursor —— colorAccent
- text color —— android:textColorPrimary
RadioButton
- unselected —— colorControlNormal
- selected —— colorAccent
CheckBox
- box unchecked —— colorControlNormal
- box checked —— colorAccent
Toolbar
- background —— 佈局文件中設置android:background="?attr/colorPrimary
- overflow icon —— android:textColorPrimary
- navigation icon —— android:textColorPrimary
- action icons —— android:textColorPrimary
- overflow menu background —— android:colorBackground
- overflow text color —— android:textColorPrimary
ActionBar
- background —— colorPrimary
- title color —— android:textColorPrimary
- overflow icon —— android:textColorPrimary
- up button —— android:textColorPrimary
- action icons —— android:textColorPrimary
- overflow menu background —— android:colorBackground
- overflow text color —— android:textColorPrimary
Spinner
- indicator (not pressed) —— colorControlNormal
- indicator (pressed) —— colorAccent
- selected entry text color —— "android:textColorPrimary
SwitchCompat
- thumb switch off —— colorSwitchThumbNormal
- thumb switch on —— colorAccent
- track overlay —— colorAccent"
作者:Android機動車
鏈接:https://juejin.im/post/5a7bb683f265da4e93116c17
來源:掘金
著作權歸作者所有。商業轉載請聯繫作者獲得授權,非商業轉載請註明出處。