例子見資源(不需要資源積分)
主要內容:
Customize the Background
更改action bar的字體顏色
Customize the Tab Indicator
Overlaying the Action Bar
Customize the Background
顯示效果圖:
實現:
-
在styles中定義用於activity的theme,並覆蓋actionBarStyle的屬性,
<style name="ChangeBackgroundTheme" parent="@android:style/Theme.Holo"> <item name="android:actionBarStyle">@style/ActionBarStyle1</item> </style>
-
覆蓋actionBarStyle的background屬性值。
<!-- 更改ActionBar的背景顏色 --> <style name="ActionBarStyle1" parent="@android:style/Widget.Holo.Light.ActionBar"> <item name="android:background">@drawable/tab_background</item> </style>
- 在AndroidManifest.xml的activity標籤中指定theme
<activity
android:theme="@style/ChangeBackgroundTheme" >
</activity>
如果使用的是分離的action bar,你應該指定 backgroundStacked,backgroundSplit屬性來指定背景。
更改action bar的字體顏色
顯示效果圖:
實現:
爲了修改action bar的文本字體顏色,你需要覆蓋各個不同文本的屬性值
(action bar的標題,Action bar tabs,Action buttons)
action bar的標題: (theme -> actionBarStyle -> titleTextStyle -> textColor )
1.創建titleTextStyle指定textColor屬性
<!--Note: The custom style applied to titleTextStyle should use TextAppearance.Holo.Widget.ActionBar.Title as the parent style. --> <!--ActionBar title style--> <style name="MyActionBarTitleText" parent="@android:style/TextAppearance.Holo.Widget.ActionBar.Title" > <item name="android:textColor">@color/actionBarTitleTextcolor</item> </style>
2.創建actionBarStyle指定titleTextStyle,
<!-- ActionBar styles --> <style name="MyActionBar" parent="@android:style/Widget.Holo.ActionBar"> <item name="android:titleTextStyle">@style/MyActionBarTitleText</item> </style>
3.定義用於activity的theme,並覆蓋actionBarStyle的屬性
<!-- 更改標題字體顏色 --> <style name="CustomTheTextColor" parent="@android:style/Theme.Holo"> <item name="android:actionBarStyle">@style/MyActionBar</item> </style>
Action bar tabs: 定義用於activity的theme,並覆蓋actionBarTabTextStyle的屬性
<!-- action tab text style -->
<style name="ActionBarTabText" parent="@android:style/Widget.ActionBar.TabText">
<item name="android:textColor">@color/tab_textColor</item>
</style>
<style name="CustomTheTextColor" parent="@android:style/Theme.Holo">
<item name="android:actionBarTabTextStyle">@style/ActionBarTabText</item>
</style>
Action buttons: 定義用於activity的theme,並覆蓋actionMenuTextColor的屬性
<style name="CustomTheTextColor" parent="@android:style/Theme.Holo"> <item name="android:actionMenuTextColor">@color/actionbarMenutextColor</item> </style>
最後,在activity中指定theme
Customize the Tab Indicator
效果圖:
-
實現:
-
.創建actionBarTabStyle並覆蓋background屬性,background應該指向一個狀態列表的drawable。
<!-- Action Bar tabs Style --> <style name="MyActionBarTabs" parent="@android:style/Widget.ActionBar.TabView"> <item name="android:background">@drawable/actionbar_tab_indicator</item> </style> <!--actionbar_tab_indicator的定義--> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:state_selected="true" android:drawable="@drawable/select"/> <item android:drawable="@drawable/normal"/> </selector>
-
定義用於activity的theme,並覆蓋actionBarTabStyle的屬性
<!-- 用於指示Tab是否選擇 --> <style name="CustomizetheTabIndicator" parent="@android:style/Theme.Holo"> <item name="android:actionBarTabStyle">@style/MyActionBarTabs</item> </style>
-
在AndroidManifest.xml中指定theme
Overlaying the Action Bar
效果:
實現:
只要開啓overlay模式就可以了,也就是說android:windowActionBarOverlay設置成true
<style name="OverlayingtheActionBar" parent="@android:style/Theme.Holo"> <item name="android:windowActionBarOverlay">true</item> <item name="android:actionBarStyle">@style/MyActionBar</item> </style> <!-- 實現Actionbar的透明度 --> <style name="MyActionBar" parent="@android:style/Widget.Holo.ActionBar"> <item name="android:background">@color/actionbar_bg</item> </style>
在什麼情況下使用?
就是在有用戶交互時,我們可能會隱藏或者顯示action bar,然而這樣會造成,這會導致你的activity
重新計算和重繪佈局,根據新的尺寸,爲了避免這種情況,你就可以開啓overlaymode。
action bar總結都是基於android 3.0,爲了支持低版本的設備,可以參閱“官方資料鏈接”的內容。
官方資料鏈接:
action bar1
action bar2