custom style action bar not working in android 4

2019-02-24 09:20发布

问题:

For customizing my action bar I use code below in xml:

<style name="CustomActionbarTheme" parent="@style/Theme.AppCompat.Light">
    <item name="actionBarTabStyle">@style/customTab</item>
    <item name="actionBarStyle">@style/MyActionBar</item>
</style>

<style name="MyActionBar" parent="@style/Widget.AppCompat.ActionBar">
    <item name="background">@drawable/title_bar</item>
</style>

<style name="customTab" parent="@style/Widget.AppCompat.ActionBar.TabView">
    <item name="android:background">@drawable/tab_bg_selector</item>
</style>

I have no problem using api level 10 but when I run my code in higher api devices, action bars are in their default theme. I use support library v7 in my project.

回答1:

Update: as of AppCompat version 21, all API levels now use the non-prefixed style names so there is no longer a need to add the android: namespaced attributes.

Now obsolete: If you are using the support library v7 ActionBar, you need to declare your custom styles twice, once without the android: prefix (for <v11 support - this is what you have) and once with (for >=v11 support - you need to add this) as per the Styling the Action Bar guide.