![](/img/trans.png)
[英]Change icon and title color when selected in android design library TabLayout
[英]Android, TabLayout icon color doesn't change when dragging
所以,我有這樣的代碼:
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時,圖標的顏色不會改變,看起來像這樣:
那不是應該怎么做。 TabLayout
可以根據選擇的圖標state_selected
從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
看起來像這樣:
<?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上,需要編程解決方案。
在這里修改前面的代碼:
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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.