Android,拖动时TabLayout图标颜色不变 [英] 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);
}
});
当我单击TabLayout图标更改页面时,一切正常,但是当我拖动viewPager时,图标的颜色不会改变,看起来像这样:
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
可以根据 state_selected
://developer.android.com/reference/android/graphics/drawable/StateListDrawable.html"rel =" nofollow> StateListDrawable
That is not how it should be done. TabLayout
can choose the icon based on state_selected
from StateListDrawable
有两种创建StateListDrawable
的方法:
1.仅限API 21+解决方案
像这样创建标签:
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);
}
其中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
是您的图像,而R.drawable.drawable_selected
看起来像这样:
R.drawable.drawable_unselected
is you image and R.drawable.drawable_selected
looks like this:
<?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+兼容解决方案
不幸的是,API 21中引入了可绘制xml中bitmap
标记内的android:tint
.在较低的API上,需要编程解决方案.
Unfortunately, android:tint
inside bitmap
tag in drawable xml has been introduced in API 21. On lower API's, programmatic solution is needed.
在这里修改以前的代码:
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);
}
用于创建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);
}
这篇关于Android,拖动时TabLayout图标颜色不变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!