Android 以编程方式更改导航选项卡的颜色

Android 以编程方式更改导航选项卡的颜色,android,android-activity,tabs,navigation,android-viewpager,Android,Android Activity,Tabs,Navigation,Android Viewpager,我试图通过编程更改操作栏选项卡的颜色。默认情况下,我在styles.xml中将它们设置为红色,这是我希望它们与我的viewpager中的其他选项卡一起使用的方式,但是,在第一个选项卡上,我希望操作栏和导航选项卡都变为透明。我已经使用这个代码通过操作栏完成了这项工作 @Override public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { // When the giv

我试图通过编程更改操作栏选项卡的颜色。默认情况下,我在styles.xml中将它们设置为红色,这是我希望它们与我的viewpager中的其他选项卡一起使用的方式,但是,在第一个选项卡上,我希望操作栏和导航选项卡都变为透明。我已经使用这个代码通过操作栏完成了这项工作

@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    // When the given tab is selected, switch to the corresponding page in
    // the ViewPager.
    int newActionBarColor/*, newActionBarTabColor*/;
    if(tab.getPosition() == 0) {
        newActionBarColor  = Color.parseColor("#" + Integer.toHexString(getResources().getColor(R.color.actionBarTransparent)));
        //newActionBarTabColor = Color.parseColor("#" + Integer.toHexString(getResources().getColor(R.color.actionBarTabsTransparent)));
    } else {
        newActionBarColor  = Color.parseColor("#" + Integer.toHexString(getResources().getColor(R.color.actionBar)));
        //newActionBarTabColor = Color.parseColor("#" + Integer.toHexString(getResources().getColor(R.color.actionBarTabs)));
    }
    getSupportActionBar().setBackgroundDrawable(new ColorDrawable(newActionBarColor));
    //getSupportActionBar().setStackedBackgroundDrawable(new ColorDrawable(newActionBarTabColor));
    //getSupportActionBar().setSplitBackgroundDrawable(new ColorDrawable(newActionBarTabColor));
    mViewPager.setCurrentItem(tab.getPosition());
}
但是我不能让它和标签一起工作,有什么想法吗?你可以看到我在上面尝试了什么

需要彩色选项卡的选项卡上的活动:

我希望操作栏和选项卡都是透明的选项卡上的当前内容:


仅支持Android 3.0及更高版本时,您可以如下定义操作栏的背景:

res/values/themes.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- the theme applied to the application or activity -->
    <style name="CustomActionBarTheme"
           parent="@android:style/Theme.Holo.Light.DarkActionBar">
        <item name="android:actionBarStyle">@style/MyActionBar</item>
    </style>

    <!-- ActionBar styles -->
    <style name="MyActionBar"
           parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
        <item name="android:background">@drawable/actionbar_background</item>
    </style>
</resources>
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>
res/values/themes.xml
@样式/MyActionBar
@可绘制/操作栏\u背景
然后将主题应用于整个应用程序或单个活动:

<application android:theme="@style/CustomActionBarTheme" ... />
<application android:theme="@style/CustomActionBarTheme" ... />

这不会改变任何事情,我也不知道该怎么办。我准确地复制到,当我将操作栏设置为红色时,它将保持默认颜色。同时,我发现以下链接可能会对您有所帮助:
http://jgilfelt.github.io/android-actionbarstylegenerator/