简体   繁体   English

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);
            }
        });

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_selectedStateListDrawable

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.

相关问题 在android设计库TabLayout中选择时更改图标和标题颜色 - Change icon and title color when selected in android design library TabLayout 如何更改 TabLayout Android 的图标颜色 - How to change icon color of TabLayout Android android中tablayout中图标颜色设置的更改方法 - How to change the icon color set in tablayout in android Android TabLayout在单击时不会更改Tab(片段) - Android TabLayout doesn't change Tab (Fragment) when clicked 具有自定义图标的TabLayout在启动时不会更改图标 - TabLayout with custom icon doesn't change icon on start 我们如何在 android 中更改 Selected Tablayout 图标和文本颜色 - How can we change Selected Tablayout Icon and Text color in android 当单击时,Android TabLayout不会更改Tab(片段),仅在滑动时才起作用 - Android TabLayout doesn't change Tab (Fragment) when clicked only works when swiped Android:tabSelectedTextColor不会更改TabLayout中所选标签的文本 - Android: tabSelectedTextColor doesn't change text of selected tab in TabLayout 带有文本和图标的Android TabLayout更改选定选项卡上的文本和图标的颜色 - Android TabLayout With Text and Icons Change color of both text and Icon on selected tab 单击 android 时更改工具栏中图标的颜色 - Change color of icon in toolbar when click in android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM