Impossible to change the background of tabs with A

2019-07-16 19:36发布

问题:

I would like to change the background of the tabs. I tried everything during the last 2 days, so I decided to post my code here. The background of the ActionBar is set correctly but the TABS stay black.

  <style name="CustomActivityTheme" parent="Theme.Sherlock">
    <item name="android:actionBarStyle">@style/MyActionBar</item>
    <item name="android:actionBarTabStyle">@style/MyActionBarTabStyle</item>
    <!-- other activity and action bar styles here -->
</style>

<!-- style for the action bar backgrounds -->
<style name="MyActionBar" parent="Widget.Sherlock.ActionBar">
    <item name="android:background">@drawable/backgroundactionbar</item>
</style>


<style name="MyActionBarTabStyle" parent="Widget.Sherlock.ActionBar.TabView">
<item name="android:background">@drawable/backgroundactionbar</item>
</style>

On the manifest:

         <activity android:name=".FragmentActivityDashboard"
              android:label="@string/app_name"
              android:configChanges="keyboardHidden|orientation|screenLayout"
              android:theme="@style/CustomActivityTheme"
              > 
    </activity>

回答1:

You need to declare two things to change. android:actionBarStyle and actionBarStyle

<!-- Theme For Tabs and ActionBar Background -->
<style name="Theme.Tabs" parent="Theme.Sherlock">
    <item name="android:actionBarTabStyle">@style/ActionBarTab</item>
    <item name="actionBarTabStyle">@style/ActionBarTab</item>

    <item name="android:actionBarStyle">@style/ActionBarBlank</item>
    <item name="actionBarStyle">@style/ActionBarBlank</item>
</style>

<!-- Styles for the above theme -->
<style name="ActionBarBlank" parent="Widget.Sherlock.ActionBar">
    <item name="android:background">@drawable/tab_bg</item>
    <item name="background">@drawable/tab_bg</item>
</style>

<style name="ActionBarTab" parent="Widget.Sherlock.ActionBar.TabView">
    <item name="android:background">@drawable/bg_tab</item>
    <item name="background">@drawable/bg_tab</item>
</style>

Note that the drawable for tabs are declared in xml file, to indicate the separate drawable for selected tab.

Here's the bg_tab:

<selector xmlns:android="http://schemas.android.com/apk/res/android">

<!-- Selected -->
<item android:state_selected="true" android:drawable="@drawable/tab_bg" />

<!-- Normal -->
<item android:drawable="@drawable/transparent" />

</selector>