TabLayout color of unselected tab underline

扶醉桌前 提交于 2019-12-12 10:23:11

问题


In this picture, in tablayout, selected tabbar underline color is purple, and text.

I search of unselected tabbar, but I couldn't find unselected tabbar underline.

I want change the color when I select some tab, change the unselected tabbar underline color.

If you know about this, would you help me?


回答1:


Create an xml file inside your drawable folder

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>

And set this drawable in your 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" />

To change the unselected tab text color, simple provide a default tab text color and selected tab text color as follows:

<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" />



回答2:


simply you can use android:background to set color for all unselected tabs once.

    <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" />


来源:https://stackoverflow.com/questions/45580036/tablayout-color-of-unselected-tab-underline

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!