动态改变switch的颜色


//动态设置switch的颜色
imgOpenSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> {
    int thumbColor;
    int trackColor;
    if(isChecked) {
        thumbColor = theme;
        trackColor = thumbColor;
    } else {
        thumbColor = Color.argb(255, 236, 236, 236);
        trackColor = Color.argb(255, 0, 0, 0);
    }
    try {
        imgOpenSwitch.getThumbDrawable().setColorFilter(new PorterDuffColorFilter(thumbColor,PorterDuff.Mode.MULTIPLY));
        imgOpenSwitch.getTrackDrawable().setColorFilter(new PorterDuffColorFilter(trackColor, PorterDuff.Mode.MULTIPLY));
    }
    catch (NullPointerException e) {
        e.printStackTrace();
    }
});

文章作者: 小游
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 小游 !
  目录