[英]How can we change Selected Tablayout Icon and Text color in android
[英]Change icon and title color when selected in android design library TabLayout
我正在使用设计库的TabLayout
,我想实现的目标是
我已经尝试了许多教程,并且可以通过自定义选项卡来实现它,但是当选择选项卡时出现了一个局限性,我想更改文本颜色以及图标的图像,这是无法通过引用获得的。到目前为止我所读的任何教程。 到目前为止,我已经通过在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;
}
设计库已更新,以匹配材料设计“带有图标和文本的制表符”规范,因此您不需要自定义选项卡视图。
但是在当前版本(23.1.1)中,只有文本颜色与规范匹配(标签集中-#fff,标签未集中-70%#fff)。 所以,你可以使用ColorStateList
返回由getTabTextColors()
来使用着色的图标DrawableCompat.setTintList(ColorStateList)
尝试使用此要点https://gist.github.com/mikovali/7a89b505cd6306bb94a8 。 删除行tabs.setTabTextColors(Color.RED, Color.GREEN)
应该足以匹配深色工具栏上的文本和图标规范。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.