Android Actionbar(標題欄)的背景設定

RockoZZ發表於2014-05-04

這東西Android官方文件是有的(點選開啟連結),這裡順便說下使用ActionbarSherlock時的設定。

For Android 3.0 and higher only(對於不需要相容2.x時可以直接如下設定)

When supporting Android 3.0 and higher only, you can define the action bar's background like this:

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>

actionbar_background背景資源如下:
<?xml version="1.0" encoding="utf-8"?>
<color xmlns:android="http://schemas.android.com/apk/res/android" android:color="#7EC0EE">
</color>

For Android 2.1 and higher(需要相容2.x時要做的如下)

When using the Support Library, the same theme as above must instead look like this:

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>

ActionbarSherlock也類似這樣做,Theme.AppCompat.Light.DarkActionBar換成Theme.Sherlock.Light;Widget.AppCompat.Light.ActionBar.Solid.Inverse換成Widget.Sherlock.Light.ActionBar.Solid.Inverse 就行了


效果:



相關文章