TabWidget当前标签底线颜色

neo*_*108 62 android android-tabs

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

mTabHost.getTabWidget().setStripEnabled(true);
mTabHost.getTabWidget().setRightStripDrawable(R.drawable.redline);
mTabHost.getTabWidget().setLeftStripDrawable(R.drawable.redline);
Run Code Online (Sandbox Code Playgroud)

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

在此输入图像描述

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

我看了这个答案,但没有说明如何改变颜色......

Mic*_*ley 76

选项卡指示器的颜色由选择器drawable设置,可在此处找到,如下所示:

<!-- 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>
Run Code Online (Sandbox Code Playgroud)

选择器使用的drawable都是浅蓝色.您可以使用自己的重新着色版本替换这些drawable.原件看起来像这样(原件很小,包括链接):

您需要将上面的选择器与drawable一起复制到您自己的项目中.然后你需要将drawables重新着色为你想要的任何颜色.然后,您需要将选择器设置为选项卡指示器的背景.您可以这样做(设置选项卡后):

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);
}
Run Code Online (Sandbox Code Playgroud)

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

  • 回答原来的答案.虽然http://android-holo-colors.com/是一个有用的网站,用于创建也会更改标签颜色的整个主题,但这个问题与创建主题无关. (2认同)

Nid*_*dhi 15

您可以使用app:tabIndicatorColor来实现此目的.它将根据您的要求更改选定的标签指示线颜色.

<android.support.design.widget.TabLayout
                    android:id="@+id/tabs"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:tabIndicatorColor="@android:color/white"
                    app:tabMode="fixed" />
Run Code Online (Sandbox Code Playgroud)


Bas*_*rif 12

这就是我改变标签的方式,

private void changetabs(TabWidget tabWidget) {
    // Change background
    for(int i=0; i < tabWidget.getChildCount(); i++)
        tabWidget.getChildAt(i).setBackgroundResource(R.drawable.tab_selector);
}
Run Code Online (Sandbox Code Playgroud)

和我的tab_selector.xml

<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" />
Run Code Online (Sandbox Code Playgroud)

希望它会帮助一些人.

  • 什么时候会改变'changetabs'? (5认同)

Ash*_*Ash 10

包括一些绊倒,有一个在线工具快速建立标签的drawables(9补丁).只需选择颜色并按下按钮在这里你去...

感谢Jeff Gilfelt


Android Action Bar Style Generator允许您轻松地为Android应用程序创建简单,有吸引力且无缝的自定义操作栏样式.它将生成所有必需的九个补丁资源以及相关的XML drawable和样式,您可以将它们直接复制到项目中.

http://jgilfelt.github.io/android-actionbarstylegenerator/


小智 5

您可以使用过滤器,
这将应用于不透明的区域

tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).getBackground().setColorFilter(Color.RED, PorterDuff.Mode.MULTIPLY);
Run Code Online (Sandbox Code Playgroud)

一行代码 - 无需更改状态.


dzi*_*kyy 5

默认情况下使用强调色作为活动选项卡颜色。您可以在style.xml文件中设置/更改它:

<style name="AppBaseTheme" parent="Theme.AppCompat.Light">

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

</style>
Run Code Online (Sandbox Code Playgroud)