通过编程更改导航选项卡的颜色

4

我想在程序中动态改变操作栏标签的颜色。默认情况下,它们在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());
}

但是我无法在选项卡中使其正常工作,有任何想法吗?您可以看到我尝试过的内容。

我想要彩色标签的选项卡上的活动:

当前我在希望操作栏和选项卡都透明的选项卡上所拥有的:

1个回答

0

当仅支持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>

然后将您的主题应用于整个应用程序或单个活动:

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

当使用支持库时,与上述相同的主题必须像这样:

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>

然后将你的主题应用于整个应用程序或单独的活动:
<application android:theme="@style/CustomActionBarTheme" ... />

您可以从以下链接获取更多信息:样式化操作栏

这并没有改变任何东西,我也不知道为什么。我完全复制了它,但当我将操作栏设置为红色时,它仍然保持默认颜色。 - user1174868
与此同时,我找到了以下链接,可能会对您有所帮助:http://jgilfelt.github.io/android-actionbarstylegenerator/ - Michele La Ferla

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接