未选中标签下划线的TabLayout颜色

3
在这张图片中,在TabLayout控件中,选中的标签栏下划线颜色为紫色,文本也是紫色。
我想找到未选中的标签栏下划线,但我找不到。
我想要在选择某个标签时改变该标签下划线颜色,并改变未选中标签栏下划线颜色。
如果您知道相关信息,请帮助我。
2个回答

9
在drawable文件夹中创建一个xml文件。 custom_indicator.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
 <!-- UNSELECTED TAB STATE -->
<item android:state_selected="false" android:state_pressed="false">
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <!-- Bottom indicator color for the UNSELECTED tab state -->
        <item android:top="-5dp" android:left="-5dp" android:right="-5dp">
            <shape android:shape="rectangle">
                <stroke android:color="#65acee" android:width="2dp"/>
            </shape>
        </item>
    </layer-list>
</item>
</selector>

并将这个drawable设置到你的tabLayout

<android.support.design.widget.TabLayout
            android:id="@+id/tab_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/white"
            app:tabGravity="fill"
            app:tabMode="fixed"
            app:tabBackground="@drawable/custom_indicator" />

要更改未选中的标签文本颜色,请提供默认标签文本颜色和选定的标签文本颜色,如下所示:
<android.support.design.widget.TabLayout
            android:id="@+id/tab_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/white"
            app:tabGravity="fill"
            app:tabMode="fixed"
            app:tabTextColor="@color/unselected_color"
            app:tabSelectedTextColor="@color/selected_color"
            app:tabBackground="@drawable/custom_indicator" />

谢谢,但在这种情况下,未选中的下划线颜色会改变,但选中的颜色不会改变。 - Polaris Nation
我认为我在custom_indicator中又添加了一个项目,并将state_selected更改为'true',但这并没有发生。你的修正是正确的。谢谢。 - Polaris Nation
谢谢!如果这对您有用,您可以将其标记为已解决。 - Ayush Khare
如果我想动态地更改未选颜色,怎么办?没有找到不经过选择器的方法。 - joghm

1

你可以使用android:background属性来为所有未选中的选项卡设置颜色。

    <style name="tab_text_style">
        <item name="android:textSize">16sp</item>
        <item name="android:fontFamily">sans-serif</item>
        <item name="android:textStyle">bold</item>
    </style>

    <style name="tab_style">
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="tabIndicatorColor">@android:color/white</item>
        <item name="tabIndicatorHeight">3dp</item>
        <item name="tabTextAppearance">@style/tab_text_style</item>
        <item name="tabSelectedTextColor">@android:color/white</item>
        <item name="tabTextColor">@color/inactive_gray</item>
        <item name="android:background">@drawable/custom_inactive_tab_indicator</item>
        <item name="tabGravity">fill</item>
        <item name="tabMode">fixed</item>
    </style>

custom_inactive_tab_indicator.xml:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:left="-4dp"
        android:right="-4dp"
        android:top="-4dp">
        <shape android:shape="rectangle">
            <stroke
                android:width="3dp"
                android:color="#57595f" />
        </shape>
    </item>
</layer-list>

activity.xml

        <com.google.android.material.tabs.TabLayout
            android:id="@+id/tabs"
            style="@style/tab_style" />

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接