注:本文翻譯自Google官方的Android Developers Training文檔,譯者技術(shù)一般,由于喜愛(ài)安卓而產(chǎn)生了翻譯的念頭,純屬個(gè)人興趣愛(ài)好。
原文鏈接: http://developer.android.com/training/basics/actionbar/styling.html
Action Bar能夠向你的用戶提供易掌握的操作方法,同時(shí)也能幫助用戶導(dǎo)航,但這不代表所有應(yīng)用的Action都長(zhǎng)一個(gè)模樣。如果你希望將你的Action Bar風(fēng)格進(jìn)行自定義來(lái)使它符合你應(yīng)用的整體風(fēng)格,你可以通過(guò)使用安卓的風(fēng)格和主題資源( style and theme )來(lái)實(shí)現(xiàn)這一設(shè)想。
Android包含一些內(nèi)置的Activity主題,包括“暗(dark)”和“亮(light)”的Action Bar風(fēng)格。你也可以將這些主題做進(jìn)一步地定制化。
Note:
如果你使用的是“ Support Library ” APIs所提供的Action Bar,那么你必須使用(或者說(shuō)覆寫(xiě)) Theme.AppCompat 這一系列中的風(fēng)格(而不是在API Level 11或以上中的 Theme.Holo 系列)。因此,每個(gè)你聲明的風(fēng)格屬性必須被聲明兩次:一次用于平臺(tái)風(fēng)格的聲明(“ android: ”屬性)還有一次用來(lái)聲明“ Support Library ”所包含的風(fēng)格的屬性( “
appcompat.R.attr ”屬性,這些屬性的Context一般就是你的App
)。可以接下去看例子來(lái)理解。
?
?一). 使用一個(gè)Android主題
Android包括兩個(gè)基本的activity主題,它們決定了Action Bar的顏色:
- Theme.Holo 對(duì)應(yīng)于“暗色”主題
- Theme.Holo.Light 對(duì)應(yīng)于“亮色”主題
你既可以通過(guò)在清單文件的
<application>
標(biāo)簽中對(duì)
android:theme
屬性標(biāo)簽進(jìn)行聲明,來(lái)
將這些主題應(yīng)用到你的整個(gè)應(yīng)用當(dāng)中,也可以在清單文件的單個(gè)
<activity>
標(biāo)簽中對(duì)
android:theme
屬性標(biāo)簽進(jìn)行聲明,來(lái)將主題
應(yīng)用到單個(gè)activity中。例如:
< application android:theme ="@android:style/Theme.Holo.Light" ... />
你也可以僅讓Action Bar為暗色,并把a(bǔ)ctivity的剩余部分設(shè)置為亮色主題,這可以通過(guò)聲明
Theme.Holo.Light.DarkActionBar
這一主題來(lái)實(shí)現(xiàn)。
當(dāng)你使用的是 Support Library 時(shí),你必須使用 Theme.AppCompat 系列的主題:
- Theme.AppCompat 對(duì)應(yīng)于“暗色”主題
- Theme.AppCompat.Light 對(duì)應(yīng)于“亮色”主題
- Theme.AppCompat.Light.DarkActionBar 對(duì)應(yīng)于擁有“暗色”Action Bar的“亮色”主題
請(qǐng)務(wù)必記得,你使用的action bar上的圖標(biāo)要和你的action bar的背景色擁有反差。在 Action Bar Icon Pack 包含了適配于“ Holo light ”和“ Holo dark ”這兩個(gè)系列主題的Action Bar的配套圖標(biāo)。
?
二). 自定義背景 色
為了改變Action Bar的背景色,你可以為你的activity創(chuàng)建一個(gè)自定義的主題,這可以通過(guò)覆寫(xiě)
actionBarStyle
這一屬性。這一屬性指向另一個(gè)style文件,在其中你可以覆寫(xiě)
background
屬性,來(lái)為
action bar
特定一個(gè)圖像資源作為其背景。如果你的應(yīng)用使用
navigation tabs
或者
split action bar
,之后你也可以分別通過(guò)使用
backgroundStacked
和
backgroundSplit
這兩個(gè)屬性字段為這些
action bar
指定背景。
Caution:
注意,你最好是為你自定義的主題和風(fēng)格聲明一個(gè)父主題,使得你的自定義的主題可以繼承父主題的風(fēng)格。如果沒(méi)有一個(gè)父主題的風(fēng)格,那么你自定義的Action Bar會(huì)缺少很多風(fēng)格屬性,除非你顯示地聲明了他們。
對(duì)于Android 3.0或更高版本的系統(tǒng)
當(dāng)只支持Android 3.0或更高系統(tǒng)版本,你可以這樣聲明你的Action Bar背景:
res/values/themes.xml
<? xml version="1.0" encoding="utf-8" ?> < resources > <!-- the theme applied to the application or activity --> < style name ="CustomActionBarTheme" parent ="@style/Theme.Holo.Light.DarkActionBar" > < item name ="android:actionBarStyle" > @style/MyActionBar </ item > </ style > <!-- ActionBar styles --> < style name ="MyActionBar" parent ="@style/Widget.Holo.Light.ActionBar.Solid.Inverse" > < item name ="android:background" > @drawable/actionbar_background </ item > </ style > </ resources >
之后將你的主題應(yīng)用到你的整個(gè)系統(tǒng)或單個(gè)activity中
< application android:theme ="@style/CustomActionBarTheme" ... />
對(duì)于Android 2.1或更高版本的系統(tǒng)
如果使用“ Support Library ”,像上述中的那個(gè)主題應(yīng)該這樣聲明:
res/values/themes.xml
<? xml version="1.0" encoding="utf-8" ?> < resources > <!-- the theme applied to the application or activity --> < style name ="CustomActionBarTheme" parent ="@style/Theme.AppCompat.Light.DarkActionBar" > < item name ="android:actionBarStyle" > @style/MyActionBar </ item > <!-- Support library compatibility --> < item name ="actionBarStyle" > @style/MyActionBar </ item > </ style > <!-- ActionBar styles --> < style name ="MyActionBar" parent ="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse" > < item name ="android:background" > @drawable/actionbar_background </ item > <!-- Support library compatibility --> < item name ="background" > @drawable/actionbar_background </ item > </ style > </ resources >
之后將你的主題應(yīng)用到你的整個(gè)系統(tǒng)或單個(gè)activity中
< application android:theme ="@style/CustomActionBarTheme" ... />
?
三). 自定義字體顏色
要修改Action Bar中的字體顏色,你需要分別為每個(gè)文本標(biāo)簽覆寫(xiě)響應(yīng)的屬性:
-
Action Bar標(biāo)題:創(chuàng)建一個(gè)指定了“
textColor
”屬性,并且在你的自定義的
actionBarStyle 中為 titleTextStyle 屬性指定了風(fēng)格。
Note:
應(yīng)用在
titleTextStyle 上的自定義風(fēng)格必須使用
TextAppearance.Holo.Widget.ActionBar.Title 作為父風(fēng)格( parent style )。
- Action Bar標(biāo)簽:在你的activity主題中,覆寫(xiě) actionBarTabTextStyle 。
- 操作按鈕: 在你的activity主題中,覆寫(xiě) actionMenuTextColor 。
對(duì)于Android 3.0或更高版本的系統(tǒng)
當(dāng)只支持Android 3.0或更高系統(tǒng)版本,你的風(fēng)格XML文件看上去應(yīng)該像是這樣:
<? xml version="1.0" encoding="utf-8" ?> < resources > <!-- the theme applied to the application or activity --> < style name ="CustomActionBarTheme" parent ="@style/Theme.Holo" > < item name ="android:actionBarStyle" > @style/MyActionBar </ item > < item name ="android:actionBarTabTextStyle" > @style/MyActionBarTabText </ item > < item name ="android:actionMenuTextColor" > @color/actionbar_text </ item > </ style > <!-- ActionBar styles --> < style name ="MyActionBar" parent ="@style/Widget.Holo.ActionBar" > < item name ="android:titleTextStyle" > @style/MyActionBarTitleText </ item > </ style > <!-- ActionBar title text --> < style name ="MyActionBarTitleText" parent ="@style/TextAppearance.Holo.Widget.ActionBar.Title" > < item name ="android:textColor" > @color/actionbar_text </ item > </ style > <!-- ActionBar tabs text styles --> < style name ="MyActionBarTabText" parent ="@style/Widget.Holo.ActionBar.TabText" > < item name ="android:textColor" > @color/actionbar_text </ item > </ style > </ resources >
對(duì)于Android 2.1或更高版本的系統(tǒng)
如果使用了“ Support Library ”,你的風(fēng)格XML文件看上去應(yīng)該像是這樣:
<? xml version="1.0" encoding="utf-8" ?> < resources > <!-- the theme applied to the application or activity --> < style name ="CustomActionBarTheme" parent ="@style/Theme.AppCompat" > < item name ="android:actionBarStyle" > @style/MyActionBar </ item > < item name ="android:actionBarTabTextStyle" > @style/MyActionBarTabText </ item > < item name ="android:actionMenuTextColor" > @color/actionbar_text </ item > <!-- Support library compatibility --> < item name ="actionBarStyle" > @style/MyActionBar </ item > < item name ="actionBarTabTextStyle" > @style/MyActionBarTabText </ item > < item name ="actionMenuTextColor" > @color/actionbar_text </ item > </ style > <!-- ActionBar styles --> < style name ="MyActionBar" parent ="@style/Widget.AppCompat.ActionBar" > < item name ="android:titleTextStyle" > @style/MyActionBarTitleText </ item > <!-- Support library compatibility --> < item name ="titleTextStyle" > @style/MyActionBarTitleText </ item > </ style > <!-- ActionBar title text --> < style name ="MyActionBarTitleText" parent ="@style/TextAppearance.AppCompat.Widget.ActionBar.Title" > < item name ="android:textColor" > @color/actionbar_text </ item > <!-- The textColor property is backward compatible with the Support Library --> </ style > <!-- ActionBar tabs text --> < style name ="MyActionBarTabText" parent ="@style/Widget.AppCompat.ActionBar.TabText" > < item name ="android:textColor" > @color/actionbar_text </ item > <!-- The textColor property is backward compatible with the Support Library --> </ style > </ resources >
?
四). 自定義標(biāo)簽欄
為了改變 navigation tabs 上使用的指引,創(chuàng)建一個(gè)覆寫(xiě) actionBarTabStyle 屬性的activity主題。這個(gè)屬性指向另一個(gè)風(fēng)格資源,在其中你覆寫(xiě) background 屬性,在這里指定一個(gè)圖像文件的狀態(tài)列表。
Note:
一個(gè) 圖像文件的狀態(tài)列表是很重要的,因?yàn)橥ㄟ^(guò)背景的不同可以表示出當(dāng)前那個(gè)標(biāo)簽指引是被選中的??梢酝ㄟ^(guò)閱讀 State List 來(lái)學(xué)習(xí)更多的關(guān)于如何創(chuàng)建圖像資源來(lái)多按鈕狀態(tài)的問(wèn)題。
例如:這里是一個(gè) 圖像文件的狀態(tài)列表,它為一個(gè)Action Bar標(biāo)簽的每一個(gè)不同狀態(tài)聲明一個(gè)特定的背景圖片:
res/drawable/actionbar_tab_indicator.xml
<? xml version="1.0" encoding="utf-8" ?> < selector xmlns:android ="http://schemas.android.com/apk/res/android" > <!-- STATES WHEN BUTTON IS NOT PRESSED --> <!-- Non focused states --> < item android:state_focused ="false" android:state_selected ="false" android:state_pressed ="false" android:drawable ="@drawable/tab_unselected" /> < item android:state_focused ="false" android:state_selected ="true" android:state_pressed ="false" android:drawable ="@drawable/tab_selected" /> <!-- Focused states (such as when focused with a d-pad or mouse hover) --> < item android:state_focused ="true" android:state_selected ="false" android:state_pressed ="false" android:drawable ="@drawable/tab_unselected_focused" /> < item android:state_focused ="true" android:state_selected ="true" android:state_pressed ="false" android:drawable ="@drawable/tab_selected_focused" /> <!-- STATES WHEN BUTTON IS PRESSED --> <!-- Non focused states --> < item android:state_focused ="false" android:state_selected ="false" android:state_pressed ="true" android:drawable ="@drawable/tab_unselected_pressed" /> < item android:state_focused ="false" android:state_selected ="true" android:state_pressed ="true" android:drawable ="@drawable/tab_selected_pressed" /> <!-- Focused states (such as when focused with a d-pad or mouse hover) --> < item android:state_focused ="true" android:state_selected ="false" android:state_pressed ="true" android:drawable ="@drawable/tab_unselected_pressed" /> < item android:state_focused ="true" android:state_selected ="true" android:state_pressed ="true" android:drawable ="@drawable/tab_selected_pressed" /> </ selector >
對(duì)于Android 3.0或更高版本的系統(tǒng)
當(dāng)只支持Android 3.0或更高系統(tǒng)版本,你的風(fēng)格XML文件看上去應(yīng)該像是這樣:
<? xml version="1.0" encoding="utf-8" ?> < resources > <!-- the theme applied to the application or activity --> < style name ="CustomActionBarTheme" parent ="@style/Theme.Holo" > < item name ="android:actionBarTabStyle" > @style/MyActionBarTabs </ item > </ style > <!-- ActionBar tabs styles --> < style name ="MyActionBarTabs" parent ="@style/Widget.Holo.ActionBar.TabView" > <!-- tab indicator --> < item name ="android:background" > @drawable/actionbar_tab_indicator </ item > </ style > </ resources >
對(duì)于Android 2.1或更高版本的系統(tǒng)
如果使用了“ Support Library ”,你的風(fēng)格XML文件看上去應(yīng)該像是這樣:
<? xml version="1.0" encoding="utf-8" ?> < resources > <!-- the theme applied to the application or activity --> < style name ="CustomActionBarTheme" parent ="@style/Theme.AppCompat" > < item name ="android:actionBarTabStyle" > @style/MyActionBarTabs </ item > <!-- Support library compatibility --> < item name ="actionBarTabStyle" > @style/MyActionBarTabs </ item > </ style > <!-- ActionBar tabs styles --> < style name ="MyActionBarTabs" parent ="@style/Widget.AppCompat.ActionBar.TabView" > <!-- tab indicator --> < item name ="android:background" > @drawable/actionbar_tab_indicator </ item > <!-- Support library compatibility --> < item name ="background" > @drawable/actionbar_tab_indicator </ item > </ style > </ resources >
?
更多資源:
更多Action Bar的風(fēng)格屬性: Action Bar
更多主題方面的知識(shí): Styles and Themes
*更多完整的Action Bar風(fēng)格: Android Action Bar Style Generator
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
