How to change AppCompatCheckBox color efficiently?

与世无争的帅哥 提交于 2021-02-19 05:41:10

问题


I found a very strange behaviour in my AppCompatCheckBox, already searched a fix for it but didn't find, that's why I decided to ask here.

I was trying to change the checked AppCompatCheckBox color programatically (as my app's color scheme changes dinamically, I can't use the XML attributes to color the CheckBox), so I used this approach:

chkTos = (AppCompatCheckBox) findViewById(R.id.checkboxTos);

ColorStateList colorStateList = new ColorStateList(
            new int[][]{
                    new int[]{-android.R.attr.state_checked},
                    new int[]{android.R.attr.state_checked},
            },
            new int[]{

                    Color.DKGRAY,
                    Color.RED,
            }
    );
chkTos.setSupportButtonTintList(colorStateList);

it worked, the checked color is now red, but the animation is now VERY SLOW. The performance is terrible! How can I fix this?

Have you experienced anything like this? I wanted to show you it in action, will try to create a gif of it! Maybe this is a bug on the support library?

Some references:

Change Circle color of radio button- Android

How to change checkbox checked color programmatically


回答1:


For anyone still struggling with this bug, instead of setting the SupportButtonTintList you have to set the ButtonTintList with the colorStateList you want. So the code will be like:

chkTos.setButtonTintList(colorStateList);



来源:https://stackoverflow.com/questions/37488550/how-to-change-appcompatcheckbox-color-efficiently

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!