【问题标题】:Changing color of Navigation Tabs programmatically以编程方式更改导航选项卡的颜色
【发布时间】:2014-02-17 12:41:52
【问题描述】:

我正在尝试以编程方式更改操作栏选项卡的颜色。默认情况下,我在 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 android-activity tabs navigation android-viewpager


    【解决方案1】:

    仅支持 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" ... />
    

    您可以从以下链接获取更多信息:Styling the Action Bar

    【讨论】:

    • 这不会改变任何东西,我也不知道怎么做。我完全复制到它,当我将操作栏设置为红色时,它会将操作栏保持为默认颜色。
    • 同时我找到了以下链接,可能对您有所帮助:http://jgilfelt.github.io/android-actionbarstylegenerator/
    猜你喜欢
    • 1970-01-01
    • 2011-07-31
    • 2013-04-14
    • 1970-01-01
    • 2017-10-29
    • 1970-01-01
    • 2020-11-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多