Android,TabLayout图标颜色在拖动时不会改变

时间:2016-09-13 17:27:01

标签: android android-viewpager android-tablayout

所以,我有这样的代码:

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时,图标的颜色不会改变,它看起来像这样: enter image description here

1 个答案:

答案 0 :(得分:4)

这不是应该怎么做的。 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中android:tint标记内的bitmap。在较低的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);
}