【问题标题】:Change ActionBar tab color programmatically以编程方式更改 ActionBar 选项卡颜色
【发布时间】:2013-08-02 14:54:13
【问题描述】:

我的应用中有以下 ActionBar 选项卡。我想知道改变颜色以匹配我的应用程序的最佳方法是什么。

  1. 每个选项卡的内容背景都不同。如何添加 为每个标签设置单独的背景颜色?
  2. 如何将浅蓝色条带颜色更改为白色以使其具有 3D 效果 看?

我看到了以下代码:

ActionBar ab = getActionBar();
//ab.setStackedBackgroundDrawable(new ColorDrawable(Color.parseColor("#0000ff")));

但是该行会将所有选项卡的颜色更改为一种颜色。

我的应用中的 Tab 代码是:

ActionBar ab = getActionBar();
        //ab.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#0000ff"))); not changing the tab color
        //ab.setStackedBackgroundDrawable(new ColorDrawable(Color.parseColor("#0000ff")));
        ab.setNavigationMode( ActionBar.NAVIGATION_MODE_TABS );

        Tab tab = ab.newTab()
                .setText( "TY1" )
                .setTabListener( 
                        new MyTabListener( this, TY1.class.getName() ) );
        ab.addTab( tab );

        tab = ab.newTab()
                .setText( "TY2" )
                .setTabListener( 
                        new MyTabListener( this, TY2.class.getName() ) );
        ab.addTab( tab );

        tab = ab.newTab()
                .setText( "ty3" )
                .setTabListener( 
                        new MyTabListener( this, TY3.class.getName() ) );
        ab.addTab( tab );

我们不胜感激。如果有人指出我正确的方向,我也可以使用 XML。

【问题讨论】:

    标签: java android android-actionbar android-tabhost


    【解决方案1】:

    您可以为每个选项卡设置自定义视图。为选项卡创建一个新的布局资源(它可以只是一个 TextView)。将其背景留空,并为选择指示器制作一个可绘制九个补丁的图形。获取LayoutInflater 使用

    LayoutInflater inflater = getSystemService(LAYOUT_INFLATER_SERVICE);
    

    然后对于每个选项卡,您可以这样做:

    Tab tab = ab.newTab()
            .setText("TY1")
            .setTabListener(new MyTabListener(this, TY1.class.getName()));
    View tabView = inflater.inflate(R.layout.my_tab_layout, null);
    tabView.setBackgroundColor(...); // set custom color
    tab.setCustomView(tabView);
    ab.addTab(tab);
    

    【讨论】:

    • 它将如何在我的代码示例中工作?假设第一个标签的绿色背景第二个标签的蓝色背景和第三个标签的黄色背景?
    • 为每个选项卡更改传递给tabView.setBackgroundColor() 的参数。
    • 对不起,这个答案的碰撞/坏死,但是布局 xml 文件的外观如何?我尝试了这个解决方案,但我不知道如何制作九个补丁。
    【解决方案2】:

    你可以通过定义自定义样式来改变ActionBar的背景颜色,如下:

    <resources>
        <style name="MyTheme" parent="@android:style/Theme.Holo.Light">
            <item name="android:actionBarStyle">@style/MyActionBar</item>
        </style>
        <style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
            <item name="android:background">ANY_HEX_COLOR_CODE</item>
        </style>
    </resources>
    

    转至:http://jgilfelt.github.io/android-actionbarstylegenerator/#name=example&compat=holo&theme=light&actionbarstyle=solid&texture=0&hairline=0&backColor=E4E4E4%2C100&secondaryColor=D6D6D6%2C100&tabColor=33B5E5%2C100&tertiaryColor=F2F2F2%2C100&accentColor=33B5E5%2C100&cabBackColor=FFFFFF%2C100&cabHighlightColor=33B5E5%2C100

    并创建您的操作栏,然后下载它。下载后,您可以在项目中使用它。

    【讨论】:

      【解决方案3】:

      对于那些正在寻找有关如何创建自定义选项卡视图然后更改选项卡格式的更详细示例代码的人,您可以在https://stackoverflow.com/a/28389366/3268329 看到我的答案。该问题与选择特定选项卡时更改选项卡文本颜色有关,但您可以轻松修改代码以更改背景或执行其他任何您需要的操作。

      https://stackoverflow.com/a/18491600/3268329(S.Thiongane 的回答)还展示了如何设置不同的后台资源。

      【讨论】:

        猜你喜欢
        • 2013-04-14
        • 1970-01-01
        • 1970-01-01
        • 2011-07-31
        • 2014-02-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多