TabWidget 当前标签底线颜色 [英] TabWidget current tab bottom line color

查看:30
本文介绍了TabWidget 当前标签底线颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 TabWidget,我已经启用并设置了 stripLeftstripRight...

mTabHost.getTabWidget().setStripEnabled(true);mTabHost.getTabWidget().setRightStripDrawable(R.drawable.redline);mTabHost.getTabWidget().setLeftStripDrawable(R.drawable.redline);

如下图所示,这不会更改当前选定选项卡 (TAB 2) 的底线颜色.

如何更改当前默认为蓝色的当前选定选项卡的底线颜色?(我猜蓝色是在 styles.xml 中的默认 AppTheme 样式中设置的.)

我看了

  • 您需要将上述选择器与可绘制对象一起复制到您自己的项目中.然后,您需要将可绘制对象重新着色为您希望它们成为的任何颜色.然后,您需要将选择器设置为选项卡指示器的背景.您可以这样做(在设置标签后):

    TabHost 主机 = (TabHost)view.findViewById(R.id.tab_host);TabWidget 小部件 = host.getTabWidget();for(int i = 0; i < widget.getChildCount(); i++) {查看 v = widget.getChildAt(i);//查找标题视图以确保这是一个指示器而不是分隔符.TextView tv = (TextView)v.findViewById(android.R.id.title);如果(电视 == 空){继续;}v.setBackgroundResource(R.drawable.your_tab_selector_drawable);}

    通过使用背景选择器设置您自己的客户指示器布局,可能有一种更简单的方法来做到这一点,但这对我来说是最简单的方法.

    I have a TabWidget for which I have enabled and set the stripLeft and stripRight...

    mTabHost.getTabWidget().setStripEnabled(true);
    mTabHost.getTabWidget().setRightStripDrawable(R.drawable.redline);
    mTabHost.getTabWidget().setLeftStripDrawable(R.drawable.redline);
    

    As you can see in the image below, this does not change the bottom line color of the currently selected tab (TAB 2).

    How can I change the bottom line color of the currently selected tab which is defaulted to blue at the moment? (I am guessing the blue color is being set in the default AppTheme style in styles.xml.)

    I looked at this answer but it does not say how to change the color...

    解决方案

    The color of the tab indicator is being set by a selector drawable which can be found here and looks like this:

    <!-- AOSP copyright notice can be found at the above link -->
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <!-- Non focused states -->
        <item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/tab_unselected_holo" />
        <item android:state_focused="false" android:state_selected="true"  android:state_pressed="false" android:drawable="@drawable/tab_selected_holo" />
    
        <!-- Focused states -->
        <item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/tab_unselected_focused_holo" />
        <item android:state_focused="true" android:state_selected="true"  android:state_pressed="false" android:drawable="@drawable/tab_selected_focused_holo" />
    
        <!-- Pressed -->
        <!--    Non focused states -->
        <item android:state_focused="false" android:state_selected="false" android:state_pressed="true" android:drawable="@drawable/tab_unselected_pressed_holo" />
        <item android:state_focused="false" android:state_selected="true"  android:state_pressed="true" android:drawable="@drawable/tab_selected_pressed_holo" />
    
        <!--    Focused states -->
        <item android:state_focused="true" android:state_selected="false" android:state_pressed="true" android:drawable="@drawable/tab_unselected_pressed_holo" />
        <item android:state_focused="true" android:state_selected="true"  android:state_pressed="true" android:drawable="@drawable/tab_selected_pressed_holo" />
    </selector>
    

    The drawables that the selector uses are all colored in that light blue. You can replace those drawables with your own recolored versions. The originals look like this (originals are small, links included):

    You'll want to copy the above selector into your own project along with the drawables. Then you'll want to recolor the drawables to whatever color you want them to be. Then you'll want to set your selector as the background for your tab indicators. You can do that like this (after setting up your tabs):

    TabHost host = (TabHost)view.findViewById(R.id.tab_host);
    TabWidget widget = host.getTabWidget();
    for(int i = 0; i < widget.getChildCount(); i++) {
        View v = widget.getChildAt(i);
    
        // Look for the title view to ensure this is an indicator and not a divider.
        TextView tv = (TextView)v.findViewById(android.R.id.title);
        if(tv == null) {
            continue;
        }
        v.setBackgroundResource(R.drawable.your_tab_selector_drawable);
    }
    

    There might be an easier way to do this by setting your own customer indicator layout with a background selector but this is what worked easiest for me.

    这篇关于TabWidget 当前标签底线颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆