我看了一下,旧版本不赞成使用许多答案。我不知道哪个属性会更改SwitchCompat小部件的颜色。我想在打开状态下同时更改打开和关闭状态的颜色,我希望白色而关闭则是另一种颜色
答案 0 :(得分:0)
您可以使用程序来执行此操作。我认为switch xml中的任何属性都无法帮助您。 但这也许可以帮助您:
switchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b==true){
//when Switch is On
finalSwitchCompat.setTrackTintList(ColorStateList.valueOf(Color.GRAY));
finalSwitchCompat.setThumbTintList(ColorStateList.valueOf(Color.GRAY));
}
else {
finalSwitchCompat.setTrackTintList(ColorStateList.valueOf(Color.WHITE));
finalSwitchCompat.setThumbTintList(ColorStateList.valueOf(Color.WHITE));
}
}
});