Search code examples
androidactionbarsherlockandroid-theme

setting custom style attribute for ActionBarSherlock navigation tabs breaks default theme


I'm trying to customize the navigation tabs in my ActionBarSherlock. If I simply set the background for the navigation tabs, the existing text styles for the tabs is lost. The text shrinks and is left justified within the tab. My understanding is that I am setting one attribute, and it is merged to the parent theme. It appears to entirely replace the parent theme. Do I have to set all attributes just to customize one thing in the navigation tabs?

Here's the code:

/res/values/themes.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <style name="MyTheme" parent="Theme.Sherlock.Light">
    <item name="android:dither">true</item>
    <item name="android:actionBarTabStyle">@style/MyActionBarStyle</item>
    <item name="actionBarTabStyle">@style/MyActionBarStyle</item>
  </style>
  <style name="MyActionBarStyle" parent="Widget.Sherlock.Light.ActionBar.TabBar">
    <item name="android:background">@drawable/my_new_bg</item>
  </style>
</resources>

androidmanifest.xml:

...
<application
        android:name=".MyApplication"
        android:hardwareAccelerated="true"
        android:icon="@drawable/app_icon"
        android:label="@string/app_name"
        android:description="@string/app_desc"
        android:theme="@style/MyTheme" >
...

Solution

  • If you look at the attributes of the style Widget.Sherlock.Light.ActionBar.TabBar, you'll notice that there is nothing text related to inherit from.

    <style name="Widget.Sherlock.ActionBar.TabBar" parent="Widget">
        <item name="android:divider">?attr/actionBarDivider</item>
        <item name="android:showDividers">middle</item>
        <item name="android:dividerPadding">12dip</item>
    </style>
    <style name="Widget.Sherlock.Light.ActionBar.TabBar" parent="Widget.Sherlock.ActionBar.TabBar">
    </style>
    

    Maybe you are looking for actionBarTabStyle and actionBarTabTextStyle?

    <item name="actionBarTabStyle">@style/Widget.Sherlock.Light.ActionBar.TabView</item>
    <item name="actionBarTabTextStyle">@style/Widget.Sherlock.Light.ActionBar.TabText</item>