[英]How to make SwitchCompat not change colors when checked?
This is how it looks like unchecked: 这是未经检查的样子:
and checked: 并检查:
checked clearly doesn't match design, so how can I make it look like unchecked, when it's checked? 明显不匹配的设计,那么如何使它看起来像未经检查的设计?
You can tint it with the same color for both statement: 您可以为这两个语句使用相同的颜色进行着色:
switchButton.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
isTouched = true;
switchButton.getThumbDrawable().setColorFilter(getResources().getColor(R.color.your_color), PorterDuff.Mode.SRC_IN);
return false;
}
});
switchButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if (isTouched) {
isTouched = false;
if (isChecked) {
switchButton.getThumbDrawable().setColorFilter(getResources().getColor(R.color.your_color), PorterDuff.Mode.SRC_IN);
}
else {
switchButton.getThumbDrawable().setColorFilter(getResources().getColor(R.color.your_color), PorterDuff.Mode.SRC_IN);
}
}
}
});
Got it! 得到它了! I had to do some research, how android finds that darker color for the track...
我不得不做一些研究,android如何找到轨道的较深颜色...
switchview.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
switchview.getThumbDrawable().clearColorFilter();
int colorPrimaryDark = getResources().getColor(R.color.colorPrimaryDark);
int r = (colorPrimaryDark >> 16) & 0xFF;
int g = (colorPrimaryDark >> 8) & 0xFF;
int b = (colorPrimaryDark >> 0) & 0xFF;
r = (r - 30 < 0) ? 0 : r - 30;
g = (g - 30 < 0) ? 0 : g - 30;
b = (b - 30 < 0) ? 0 : b - 30;
int darker = Color.rgb(r, g, b);
switchview.getTrackDrawable().setColorFilter(darker, PorterDuff.Mode.SRC_IN);
}
});
Now checked and unchecked will be exactly the same. 现在选中和未选中将完全相同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.