[英]Android, TabLayout icon color doesn't change when dragging
So, I have code like this: 所以,我有这样的代码:
tabLayout.setOnTabSelectedListener(
new TabLayout.ViewPagerOnTabSelectedListener(tabViewPager) {
@Override
public void onTabSelected(TabLayout.Tab tab) {
super.onTabSelected(tab);
//set selected icon color
Drawable icon = tab.getIcon();
icon = DrawableCompat.wrap(icon);
DrawableCompat.setTint(icon, ContextCompat.getColor(MainActivity.this,
R.color.colorAccent));
tab.setIcon(icon);
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
super.onTabUnselected(tab);
//set unselected icon color
Drawable icon = tab.getIcon();
icon = DrawableCompat.wrap(icon);
DrawableCompat.setTint(icon, ContextCompat.getColor(MainActivity.this,
R.color.white));
tab.setIcon(icon);
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
super.onTabReselected(tab);
}
});
When I click on TabLayout icon to change page, everything is ok, but when I drag viewPager, the color of icon doesn't change and it looks like this: 当我单击TabLayout图标以更改页面时,一切正常,但是当我拖动viewPager时,图标的颜色不会改变,看起来像这样:
That is not how it should be done. 那不是应该怎么做。
TabLayout
can choose the icon based on state_selected
from StateListDrawable
TabLayout
可以根据选择的图标state_selected
从StateListDrawable
There are two ways for creating StateListDrawable
: 有两种创建
StateListDrawable
:
1. API 21+ only solution 1.仅API 21+解决方案
Create your tabs like that: 像这样创建标签:
mTabLayout = (TabLayout) findViewById(R.id.tab_layout);
for (int i = 0; i < 4; ++i) {
TabLayout.Tab tab = mTabLayout.newTab();
tab.setIcon(R.drawable.icon);
mTabLayout.addTab(tab, i);
}
where R.drawable.icon
is a state drawable: 其中
R.drawable.icon
是可绘制状态:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/drawable_unselected" android:state_selected="false"/>
<item android:drawable="@drawable/drawable_selected" android:state_selected="true"/>
</selector>
R.drawable.drawable_unselected
is you image and R.drawable.drawable_selected
looks like this: R.drawable.drawable_unselected
是图像, R.drawable.drawable_selected
看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<bitmap
android:src="@drawable/drawable_unselected"
android:tint="@color/answer_color"/>
</item>
</layer-list>
2. API 4+ Compatible solution 2. API 4+兼容解决方案
Unfortunately, android:tint
inside bitmap
tag in drawable xml has been introduced in API 21. On lower API's, programmatic solution is needed. 不幸的是,API 21中引入了可绘制xml中
bitmap
标记内的android:tint
。在较低的API上,需要编程解决方案。
Here modify the previous code: 在这里修改前面的代码:
mTabLayout = (TabLayout) findViewById(R.id.tab_layout);
for (int i = 0; i < 4; ++i) {
TabLayout.Tab tab = mTabLayout.newTab();
StateListDrawable stateDrawable = new StateListDrawable();
Drawable unSelectedDrawable = ContextCompat.getDrawable(this, R.drawable.drawable_unselected);
Drawable selectedDrawable = createdSelectedDrawable(this, R.drawable.drawable_unselected);
stateDrawable.addState(new int[]{-android.R.attr.state_selected}, unSelectedDrawable);
stateDrawable.addState(new int[]{android.R.attr.state_selected}, selectedDrawable);
tab.setIcon(stateDrawable);
mTabLayout.addTab(tab, i);
}
The function for creating state_selected
: 创建
state_selected
的函数:
private Drawable createdSelectedDrawable(Context context, int iconResource) {
Bitmap one = BitmapFactory.decodeResource(getResources(), iconResource);
Bitmap oneCopy = Bitmap.createBitmap(one.getWidth(), one.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(oneCopy);
Paint p = new Paint();
p.setColorFilter(new PorterDuffColorFilter(ContextCompat.getColor(context, android.R.color.holo_red_dark), PorterDuff.Mode.SRC_ATOP));
c.drawBitmap(one, 0, 0, p);
return new BitmapDrawable(getResources(), oneCopy);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.