標籤:
1.設定action bar的背景色
第一種方式:getActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.ActionBarBackground));
ActionBarBackground 為定義在values/colors.xml
colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<drawable name="ActionBarBackground">#ED4026</drawable>
</resources>
此種方法特點:簡單方便,但必須在代碼中設定,若不同activity公用同一個actionbar樣式,則不利於複用 。 so, 參考第二種方式
第二種方式:由官方文檔給出,通過設定主題的屬性實現。(不同activity可複用)
具體做法: (以下方法適合3.0及以上,2.1及以上參考官方文檔)
res/values/themes.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme"
parent="@android:style/Theme.Holo.Light.DarkActionBar">
<item name="android:actionBarStyle">@style/MyActionBar</item>
</style>
<!-- ActionBar styles -->
<style name="MyActionBar"
parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
<item name="android:background">@drawable/actionbar_background</item>
</style>
</resources>
Then apply your theme to your entire app or individual activities:
<application android:theme="@style/CustomActionBarTheme" ... />
也可在建立項目後application或activity預設引用的主題樣式檔案styles.xml中直接修改添加
修改 <style name="AppBaseTheme" parent="@android:style/Theme.Holo">
或添加以上檔案內容到styles.xml
actionbar_background可添加在colors.xml檔案中
參考:http://blog.csdn.net/jwzhangjie/article/details/28852967
參考:http://blog.csdn.net/yueqinglkong/article/details/20876401
Android ActionBar自訂