Change background color of Tabs in ActionBar in Sh

2019-02-20 23:17发布

问题:

I am working on Actionbar using Sherlock library. I want to change the Background Image of the Actionbar and Tabs differently. Currently I have changed the Background of the Actionbar successfully via codes at runtime:

Drawable myIcon = getResources().getDrawable(R.drawable.blue_menu_background);
mActionBar.setBackgroundDrawable(myIcon);

But I am getting problem with the Tab background. How can I change the Background of Tabs. Please suggest me.

回答1:

Instead of doing that, you can use Style for Tabs:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- the theme applied to the application or activity -->
    <style name="CustomActivityTheme" parent="@android:style/Theme.Holo">
        <item name="android:actionBarTabTextStyle">@style/CustomTabTextStyle</item>
        <item name="android:actionBarDivider">@drawable/ab_divider</item>
        <item name="android:actionBarItemBackground">@drawable/ab_item_background</item>
    </style>

    <!-- style for the action bar tab text -->
    <style name="CustomTabTextStyle" parent="@android:style/TextAppearance.Holo">
        <item name="android:textColor">#2456c2</item>
    </style>
</resources>

check this article for more details.



回答2:

you can use this code:

actionBar.setStackedBackgroundDrawable(new ColorDrawable(yourColor)); // for example new ColorDrawable(Color.BLUE)


回答3:

To Change Actionbar Tab's color, Pls use this code:

//For Example if you want White color as Tabs background, then

getActionBar().setStackedBackgroundDrawable(new ColorDrawable(Color.parseColor("#FFFFFF")));