本文介绍了设置为tabSelectedTextColor使用TabLayout选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

继codeS的工作,因为我加入了 tabSelectedTextColor 属性直接与选定的文本颜色为白色。

 < android.support.design.widget.TabLayout
            ...
            应用:tabSelectedTextColor =@色/白
            应用:tabTextColor =@色/ tab_layout/>

但下面codeS不工作,我不知道为什么,也许这是一个错误!

 < android.support.design.widget.TabLayout
            ...
            应用:tabTextColor =@色/ tab_layout/>

@色/ tab_layout

 <?XML版本=1.0编码=UTF-8&GT?;
<选择的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
    ;<! - GT& - 定义所以最好使用它选中状态
    <项目的android:颜色=@色/白机器人:state_selected =真/>
    <项目的android:颜色=@色/白机器人:state_focused =真/>
    <项目的android:颜色=@色/白机器人:STATE_ pressed =真/>
    <项目的android:颜色=#CCFFFFFF/>
< /选择>

注意: #CCFFFFFF 色彩的作品所以这意味着,有观点得到 tabTextColor 属性值正确,但不能识别的android:state_selected 项。我测试了所有理性的状态,但毫无效果。

TabLayout.class

继codeS由 TabLayout.class 复制,一切都清楚了。难道你不认为让所选文本的颜色从选择是更好的办法?如果是,请报告。

 如果(a.hasVa​​lue(styleable.TabLayout_tabSelectedTextColor)){
    的int选定= a.getColor(styleable.TabLayout_tabSelectedTextColor,0);
    this.mTabTextColors = createColorStateList(this.mTabTextColors.getDefaultColor(),选择);
}


解决方案

如果要更改选定文本的颜色,然后使用TabLayout类setTabTextColors梅索德是这样的:

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

Following codes work because I added the tabSelectedTextColor attribute directly and selected text color will be white.

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

But following codes don't work and I don't know why, maybe it 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's 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 gets the tabTextColor attribute value correctly but doesn't recognize the android:state_selected item. I tested all rational states but nothing worked.

TabLayout.class

Following codes copied from TabLayout.class and everything is clear. Don't you think getting selected text color from the selector is better way? If it is, please report it.

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

If you want to change the selected text color then use setTabTextColors methode of TabLayout class like this:

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

这篇关于设置为tabSelectedTextColor使用TabLayout选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 23:03