【问题标题】:Changing ActionBar tabs underline color programmatically以编程方式更改 ActionBar 选项卡下划线颜色
【发布时间】:2013-04-14 21:23:49
【问题描述】:

我已经通过

创建了操作栏
ActionBar actionbar = getActionBar()

action bar的背景被改变了

actionbar.setBackgroundDrawable(actionBarBackgroundImage);

现在我需要以编程方式更改操作栏选项卡的下划线颜色。有什么方法可以改变操作栏标签的下划线颜色吗?

【问题讨论】:

  • 这些答案都不适合你吗?
  • 此处指定的代码只能使用 xml 布局。我需要通过 Java 代码更改 ActionBar 颜色。是否有任何选项可以更改操作栏默认的蓝色下划线颜色?
  • @Karthick 你解决过这个问题吗?认为这是不可能的,这非常令人失望。
  • @Karthick 你有解决方案吗,如果是,请告诉我,我也在通过 JAVA 代码搜索相同的内容,我使用 setBackgroundDrawable(..) 更改 ActionBar 背景颜色
  • 你找到答案了吗? @Karthick 我也面临同样的问题,请告诉我您的解决方案

标签: android android-actionbar android-tabs


【解决方案1】:

您也可以使用Android Action Bar Style Generator 轻松地为您的操作栏和标签设置主题。

【讨论】:

    【解决方案2】:

    这是一个更简单的方法。我知道您正在寻找一种程序化的改变,但这真的很容易。

    我已经为此苦苦挣扎了好几天,但终于找到了解决方案。我正在使用 AppCompat。您可以在您的主题中设置colorAccent,这将改变您的ActionBar 上的突出显示颜色。像这样:

    <item name="colorAccent">@color/highlightcolor</item>
    

    这里是上下文:

    <style name="LightTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorPrimary">@color/darkgrey</item>
        <item name="colorPrimaryDark">@color/black</item>
        <item name="colorAccent">@color/highlightcolor</item>
    </style>
    

    我最初发布此答案的位置:Android Tab underline color not changing

    【讨论】:

    • 如果你的最小 API 是
    • 这就是 AppCompat 正在为您做的事情。您的项目的最低 API 低于 21,并且 AppCompat 为在低于 21 操作系统的系统上运行的应用提供 api-21 级别的行为。
    【解决方案3】:

    我建议你使用ActionBarSherlock。库中有一个名为“Style ActionBar”的示例。 (这是您更改 ActionBar 选项卡下划线颜色的唯一方法)

    如果你已经自定义了 ActionBar 那么你必须在 ActionBar Style 中添加这个样式

    或者这里是如何做到这一点的方法

    创建如下样式(这里我使用了 ActionBarShareLock,如果您不想使用,请使用 android-support-v4.jar 支持所有 Android 操作系统版本)

    <style name="Theme.AndroidDevelopers" parent="Theme.Sherlock.Light">
            <item name="android:actionBarTabStyle">@style/MyActionBarTabStyle</item>
            <item name="actionBarTabStyle">@style/MyActionBarTabStyle</item>
        </style>
    
        <!-- style for the tabs -->
        <style name="MyActionBarTabStyle" parent="Widget.Sherlock.Light.ActionBar.TabBar">
            <item name="android:background">@drawable/actionbar_tab_bg</item>
            <item name="android:paddingLeft">32dp</item>
            <item name="android:paddingRight">32dp</item>
    

    actionbar_tab_bg.xml

    <item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/ad_tab_unselected_holo" />
    <item android:state_focused="false" android:state_selected="true"  android:state_pressed="false" android:drawable="@drawable/ad_tab_selected_holo" />
    <item android:state_selected="false" android:state_pressed="true" android:drawable="@drawable/ad_tab_selected_pressed_holo" />
    <item android:state_selected="true"  android:state_pressed="true" android:drawable="@drawable/ad_tab_selected_pressed_holo" />
    

    在您的 android 清单文件中的活动中应用此样式

    <activity
                android:name="com.example.tabstyle.MainActivity"
                android:label="@string/app_name"
                android:theme="@style/Theme.AndroidDevelopers" >
    

    更多详情请查看answerarticle


    编辑:29-09-2015

    ActionBarSherlock 已弃用,因此您也可以将 android design 支持库和 android 应用程序 appcompat 库用于 TOOLBAR(Action-Bar 已被弃用......)和 TABS。

    使用TabLayout,如下所示

    <android.support.design.widget.TabLayout
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:tabGravity="center"
                app:tabMode="scrollable"
                app:tabSelectedTextColor="@color/white"
                app:tabIndicatorColor="@color/colorPrimary"
                app:tabIndicatorHeight="2dip"
                app:tabTextAppearance="?android:attr/textAppearanceMedium"
                app:tabTextColor="@color/colorAccent" />
    

    here is sample of android design support library with tab

    【讨论】:

    【解决方案4】:

    请参考this,自定义操作栏,

    <?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:actionBarStyle">@style/MyActionBar</item>
            <!-- other activity and action bar styles here -->
        </style>
    
        <!-- style for the action bar backgrounds -->
        <style name="MyActionBar" parent="@android:style/Widget.Holo.ActionBar">
            <item name="android:background">@drawable/ab_background</item>
            <item name="android:backgroundStacked">@drawable/ab_background</item>
            <item name="android:backgroundSplit">@drawable/ab_split_background</item>
        </style>
    </resources>
    

    【讨论】:

    • 感谢西诺的回复。我知道这种自定义操作栏的方式。但我需要通过代码自定义操作栏选项卡。
    • @Karthick - 你修好了吗?
    【解决方案5】:

    我尝试了这里和其他地方发布的许多建议,但都没有成功。但我想我设法拼凑出一个(尽管不是完美的)解决方案。

    TabWidget 正在使用选择器。本质上,它根据选项卡的状态(选择、按下等)显示不同的 9 个补丁图像。我终于发现您可以通过编程方式生成选择器。我从http://android-holo-colors.com/ 生成 9 个补丁开始(颜色:#727272,TabWidget:是)。

    最大的问题是设置颜色。设置彩色滤光片没有任何作用。所以,我最终在一个循环中更改了 9 个补丁图像的每个像素的颜色。

    ...    
    /**
     * <code>NinePatchDrawableUtility</code> utility class for manipulating nine patch resources.
     * 
     * @author amossman
     *
     */
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public class NinePatchDrawableUtility {
    
        // Matches the colors in the supported drawables
        private static final int TAB_UNDERLINE_HIGHLIGHT_COLOR = 1417247097;
        private static final int TAB_UNDERLINE_COLOR = -8882056;
        private static final int TAB_PRESSED_COLOR = -2122745479;
    
        private Resources resources;
    
        public NinePatchDrawableUtility(Resources resources) {
            this.resources = resources;
        }
    
        /**
         * Create a <code>StateListDrawable</code> that can be used as a background for the {@link android.widget.TabWidget}</br></br>
         * 
         * <code>
         * FragmentTabHost tabHost = ...</br>
         * NinePatchUtility ninePatchUtility = new NinePatchUtility(getResources());</br>
         * TabWidget tabWidget =  tabHost.getTabWidget();</br>
         * for (int i = 0; i < tabWidget.getChildCount(); i++) {</br>
         * &nbsp;&nbsp;&nbsp;tabWidget.getChildAt(i).setBackground(ninePatchUtility.getTabStateListDrawable(titleColor));</br>
         * }
         * </code>
         * 
         * @param tintColor The color to tint the <code>StateListDrawable</code>
         * @return A new <code>StateListDrawable</code> that has been tinted to the given color
         */
        public StateListDrawable getTabStateListDrawable(int tintColor) {
            StateListDrawable states = new StateListDrawable();
            states.addState(new int[] {android.R.attr.state_pressed},
                changeTabNinePatchColor(resources, R.drawable.cc_tab_selected_pressed_holo, tintColor));
            states.addState(new int[] {android.R.attr.state_focused},
                changeTabNinePatchColor(resources, R.drawable.cc_tab_selected_focused_holo, tintColor));
            states.addState(new int[] {android.R.attr.state_selected},
                changeTabNinePatchColor(resources, R.drawable.cc_tab_selected_holo, tintColor));
            states.addState(new int[] { },
                changeTabNinePatchColor(resources, R.drawable.cc_tab_unselected_holo, tintColor));
            return states;
        }
    
        /**
         * Change the color of the tab indicator.</br></br>
         * 
         * Supports only the following drawables:</br></br>
         * 
         * R.drawable.cc_tab_selected_pressed_holo</br>
         * R.drawable.cc_tab_selected_focused_holo</br>
         * R.drawable.cc_tab_selected_holo</br>
         * R.drawable.cc_tab_unselected_holo</br></br>
         * 
         * Note: This method is not efficient for large <code>Drawable</code> sizes.
         * 
         * @param resources Contains display metrics and image data
         * @param drawable The nine patch <code>Drawable</code> for the tab
         * @param tintColor The color to tint the <code>Drawable</code>
         * @return A new <code>NinePatchDrawable</code> tinted to the given color
         */
        public NinePatchDrawable changeTabNinePatchColor(Resources resources, int drawable, int tintColor) {
    
            int a = Color.alpha(tintColor);
            int r = Color.red(tintColor);
            int g = Color.green(tintColor);
            int b = Color.blue(tintColor);
            BitmapFactory.Options opt = new BitmapFactory.Options();
            opt.inMutable = true;
            Bitmap bitmap = BitmapFactory.decodeResource(resources, drawable, opt);
            for (int x = 0; x < bitmap.getWidth(); x++) {
                for (int y = 0; y < bitmap.getHeight(); y++) {
                    int color = bitmap.getPixel(x, y);
                    if (color == TAB_PRESSED_COLOR) {
                        bitmap.setPixel(x, y, Color.argb((int)(a * 0.5), r, g, b));
                    } else if (color == TAB_UNDERLINE_HIGHLIGHT_COLOR) {
                        bitmap.setPixel(x, y, Color.argb((int)(a * 0.9), r, g, b));
                    } else if (color == TAB_UNDERLINE_COLOR) {
                        bitmap.setPixel(x, y, tintColor);
                    }
                }
            }
            return new NinePatchDrawable(resources, bitmap, bitmap.getNinePatchChunk(), new Rect(), null);
        }
    
    }
    

    使用示例:

    /**
     * Theme the tab widget with the defined background color and title color set
     * in the TabManager
     * @param tabWidget
     */
    @SuppressWarnings("deprecation")
    @SuppressLint("NewApi")
    public void theme(TabWidget tabWidget) {
        ColorDrawable backgroundDrawable = new ColorDrawable(backgroundColor);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            tabWidget.setBackground(backgroundDrawable);
            tabWidget.setAlpha(0.95f);
        } else {
            backgroundDrawable.setAlpha(242);
            tabWidget.setBackgroundDrawable(backgroundDrawable);
        }
        NinePatchDrawableUtility ninePatchUtility = new NinePatchDrawableUtility(resources);
        for (int i = 0; i < tabWidget.getChildCount(); i++) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                tabWidget.getChildAt(i).setBackground(ninePatchUtility.getTabStateListDrawable(titleColor));
            } else {
                tabWidget.getChildAt(i).setBackgroundDrawable(ninePatchUtility.getTabStateListDrawable(titleColor));
            }
            View tabView = tabWidget.getChildTabViewAt(i);
            tabView.setPadding(0, 0, 0, 0);
            TextView tv = (TextView) tabView.findViewById(android.R.id.title);
            tv.setSingleLine(); // set the texts on the tabs to be single line
            tv.setTextColor(titleColor);
        }
    }
    

    【讨论】:

    • 你能举例说明如何使用这个类来设置标签选择的颜色吗? changeTabNinePathColor() 中使用了哪个drawable?
    • 有关如何使用 changeTabNinePatchColor 的示例,请参阅上述帖子中的 getTabStateListDrawable。如果要设置选项卡的整个状态列表,可以设置 tabWidget.getChildAt(i).setBackground(ninePatchUtility.getTabStateListDrawable(color) 或 setBackgroundDrawable(取决于 SDK 版本)。
    【解决方案6】:

    经过 1 天漫长的搜索后,获得了更改标签突出显示颜色的解决方案。只需 2 行代码即可完美完成这项工作!

    转到 values/styles.xml 并在 ActionBar Theme 中添加以下代码

    &lt;item name="colorAccent"&gt;@color/Tab_Highlighter&lt;/item&gt;

    现在在 colors.xml

    中为 Tab_Highlighter 提供颜色
    <color name="Tab_Highlighter">#ffffff</color>
    

    【讨论】:

    • 如果此代码有任何问题,请发表评论。它对我很好
    • 这些问题是 3 年前提出的,我认为它仍然与 OP 无关。
    • 我试图更改标签荧光笔。尝试了这里给出的所有解决方案,但没有任何效果。该解决方案有效,因此我认为这可能会帮助寻找解决方案的人。
    【解决方案7】:

    尝试关注。

    在 res/drawable 中写入 tabs_selector_green.xml。

        <!-- Non focused states -->
    <item android:drawable="@android:color/transparent" android:state_focused="false" android:state_pressed="false" android:state_selected="false"/>
    <item android:drawable="@drawable/layer_bg_selected_tabs_green" android:state_focused="false" android:state_pressed="false" android:state_selected="true"/>
    
    <!-- Focused states -->
    <item android:drawable="@android:color/transparent" android:state_focused="true" android:state_pressed="false" android:state_selected="false"/>
    <item android:drawable="@drawable/layer_bg_selected_tabs_green" android:state_focused="true" android:state_pressed="false" android:state_selected="true"/>
    
    <!-- Pressed -->
    <!-- Non focused states -->
    <item android:drawable="@android:color/transparent" android:state_focused="false" android:state_pressed="true" android:state_selected="false"/>
    <item android:drawable="@drawable/layer_bg_selected_tabs_green" android:state_focused="false" android:state_pressed="true" android:state_selected="true"/>
    
    <!-- Focused states -->
    <item android:drawable="@android:color/transparent" android:state_focused="true" android:state_pressed="true" android:state_selected="false"/>
    <item android:drawable="@drawable/layer_bg_selected_tabs_green" android:state_focused="true" android:state_pressed="true" android:state_selected="true"/>
    

    将 layer_bg_selected_tabs_green.xml 写入 res/drawable 文件夹。

    <item>
        <shape android:shape="rectangle" >
            <solid android:color="@color/tab_green" />
    
            <padding android:bottom="5dp" />
        </shape>
    </item>
    <item>
        <shape android:shape="rectangle" >
            <solid android:color="#FFFFFF" />
        </shape>
    </item>
    

    在java代码中写这个。

    private static final int[] TABS_BACKGROUND = {
            R.drawable.tabs_selector_orange, R.drawable.tabs_selector_green,
            R.drawable.tabs_selector_red, R.drawable.tabs_selector_blue,
            R.drawable.tabs_selector_yellow };
    /*
    BLA BLA BLA
    */
    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub
        RelativeLayout tabLayout = (RelativeLayout) tab.getCustomView();
        tabLayout.setBackgroundResource(TABS_BACKGROUND[tab.getPosition()]);
        tab.setCustomView(tabLayout);
    /* ... */
    }
    

    【讨论】:

      【解决方案8】:

      您可以使用此代码:

      actionBar.setStackedBackgroundDrawable(new ColorDrawable(yourColor));

      【讨论】:

      • 它为标签的整个背景着色。你能告诉我如何改变下划线的颜色吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-31
      • 2014-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多