TabWidget当前选项卡的底线颜色 [英] TabWidget current tab bottom line color

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

问题描述

我有一个TabWidget为此我已经启用并设定 stripLeft stripRight ...

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);

正如你可以在下面的图片中看到,这并不会改变当前选定的选项卡(TAB 2)的底线颜色。

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

我怎样才能改变当前所选选项卡的底线颜色,这是默认为蓝色的时刻? (我猜的蓝色在默认被设置 AppTheme 风格 styles.xml

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.)

我看了看<一href="http://stackoverflow.com/questions/3543478/remove-the-bottom-line-border-in-tab-bar-and-change-selected-color">this回答,但它并没有说如何改变颜色......

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

推荐答案

标签指示的颜色是由一个选择绘制可以找到<一集href="https://github.com/android/platform_frameworks_base/blob/master/core/res/res/drawable/tab_indicator_holo.xml">here ,看起来是这样的:

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):

  • <一个href="https://github.com/android/platform_frameworks_base/blob/master/core/res/res/drawable-mdpi/tab_unselected_holo.9.png">tab_unselected_holo
  • <一个href="https://github.com/android/platform_frameworks_base/blob/master/core/res/res/drawable-mdpi/tab_selected_holo.9.png">tab_selected_holo
  • <一个href="https://github.com/android/platform_frameworks_base/blob/master/core/res/res/drawable-mdpi/tab_unselected_focused_holo.9.png">tab_unselected_focused_holo
  • <一个href="https://github.com/android/platform_frameworks_base/blob/master/core/res/res/drawable-mdpi/tab_selected_focused_holo.9.png">tab_selected_focused_holo
  • <一个href="https://github.com/android/platform_frameworks_base/blob/master/core/res/res/drawable-mdpi/tab_unselected_$p$pssed_holo.9.png">tab_unselected_$p$pssed_holo
  • <一个href="https://github.com/android/platform_frameworks_base/blob/master/core/res/res/drawable-mdpi/tab_selected_$p$pssed_holo.9.png">tab_selected_$p$pssed_holo
  • tab_unselected_holo
  • tab_selected_holo
  • tab_unselected_focused_holo
  • tab_selected_focused_holo
  • tab_unselected_pressed_holo
  • tab_selected_pressed_holo

您会希望上述选择复制到自己的项目随着可绘制。然后,你要重新着色的任何颜色,你希望他们成为可绘制。然后,你要设置你的选择作为背景的​​标签指标。你可以做这样的(设置你的标签后):

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天全站免登陆