未选中选项卡下划线的 TabLayout 颜色

TabLayout color of unselected tab underline

在这张图片中,在tablayout中,selected tabbar下划线颜色为紫色,文字。

我搜索了未selected 的标签栏,但找不到未selected 的标签栏下划线。

我想在 select 某些选项卡时更改颜色,更改未 select 编辑的选项卡栏下划线颜色。

如果你知道这件事,你会帮助我吗?

在您的可绘制文件夹中创建一个 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>

并在您的 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" />

简单地说,您可以使用 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" />