在android设计库TabLayout中选择时更改图标和标题颜色 [英] Change icon and title color when selected in android design library TabLayout

查看:465
本文介绍了在android设计库TabLayout中选择时更改图标和标题颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用设计库的TabLayout我要实现的目标

I am using TabLayout of design library what i want to acheive is

我已经尝试了许多教程和我能够通过定制的标签,以实现它,但存在着发生被选择的标签时,我想改变文字颜色以及图标的图像是不是一个限制可以参考我到目前为止所读的任何教程.到目前为止,我已经在FragmentStatePagerAdapter

I have tried many tutorial and i am able to achieve it by customizing the tab but there is an limitation that is occurring when the tab is selected i want to change the text color as well as the image of the icon which is not possible by referring any of the tutorial i read so far. I have tried this so far by adding this in the FragmentStatePagerAdapter

public View getTabView(int position) {
    View tab = LayoutInflater.from(mContext).inflate(R.layout.tabbar_view, null);
    TextView tabText = (TextView) tab.findViewById(R.id.tabText);
    ImageView tabImage = (ImageView) tab.findViewById(R.id.tabImage);
    tabText.setText(mFragmentTitles.get(position));
    tabImage.setBackgroundResource(mFragmentIcons.get(position));
    if (position == 0) {
        tab.setSelected(true);
    }
    return tab;
}

推荐答案

设计库已更新,可以与材料设计带有图标和文本的制表符"规范相匹配,因此您不需要自定义选项卡视图.

The Design Library were updated to match the material design "Tabs with icons and text" specs, so you don't need a custom tab view.

但是在当前版本(23.1.1)中,只有文本颜色与规范匹配(标签集中-#fff,标签未集中-70%#fff).因此,您可以使用getTabTextColors()返回的ColorStateList来使用DrawableCompat.setTintList(ColorStateList)着色图标.

But in the current version (23.1.1), only the text colors match the spec (tab focused - #fff, tab unfocused - 70% #fff). So you can use the ColorStateList returned by getTabTextColors() to tint the icons using DrawableCompat.setTintList(ColorStateList).

尝试使用此要点 https://gist.github.com/mikovali/7a89b505cd6306bb94a8 .删除tabs.setTabTextColors(Color.RED, Color.GREEN)行应足以匹配深色工具栏上的文本和图标规范.

Try to use this gist https://gist.github.com/mikovali/7a89b505cd6306bb94a8. Removing the line tabs.setTabTextColors(Color.RED, Color.GREEN) should be enough to match the spec for both text and icon on dark toolbars.

这篇关于在android设计库TabLayout中选择时更改图标和标题颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆