Set tabSelectedTextColor for TabLayout using selector

The following codes work because I added an attribute tabSelectedTextColor

and the selected text color will be white.

<android.support.design.widget.TabLayout
            ...
            app:tabSelectedTextColor="@color/white"
            app:tabTextColor="@color/tab_layout"/>

      

But the following codes don't work and I don't know why, maybe this is a bug!

<android.support.design.widget.TabLayout
            ...
            app:tabTextColor="@color/tab_layout"/>

      

@ color / tab_layout

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Selected state defined so it better to use it -->
    <item android:color="@color/white" android:state_selected="true"/>
    <item android:color="@color/white" android:state_focused="true"/>
    <item android:color="@color/white" android:state_pressed="true"/>
    <item android:color="#CCFFFFFF"/>
</selector>

      

Note: #CCFFFFFF

color works, so it means the view is getting the attribute value tabTextColor

correctly, but it doesn't recognize the element android:state_selected

. I tested all rational states but nothing worked.

TabLayout.class

The following codes are copied from TabLayout.class

and everything is clear. Don't you think it's better to pick the highlighted text color from the selector? If so, please report it.

if(a.hasValue(styleable.TabLayout_tabSelectedTextColor)) {
    int selected = a.getColor(styleable.TabLayout_tabSelectedTextColor, 0);
    this.mTabTextColors = createColorStateList(this.mTabTextColors.getDefaultColor(), selected);
}

      

+3


source to share


1 answer


If you want to change the selected text color, use the setTabTextColors method of the TabLayout class as follows:



tabLayout.setTabTextColors(Color.parseColor("#ADABAE"), Color.parseColor("#FFFFFF"));

      

+2


source







All Articles