Android正在更改开关状态的颜色
我看了看,许多答案都不赞成旧版本。我想知道哪个属性会改变SwitchCompat小部件的颜色。我希望在打开时同时更改打开和关闭状态的颜色,我希望白色,而关闭时将是另一种颜色您可以使用程序来执行此操作。我认为switchxml中的任何属性都不能帮助您。 但也许这可以帮助你:Android正在更改开关状态的颜色,android,android-widget,android-switch,Android,Android Widget,Android Switch,我看了看,许多答案都不赞成旧版本。我想知道哪个属性会改变SwitchCompat小部件的颜色。我希望在打开时同时更改打开和关闭状态的颜色,我希望白色,而关闭时将是另一种颜色您可以使用程序来执行此操作。我认为switchxml中的任何属性都不能帮助您。 但也许这可以帮助你: switchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public v
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));
}
}
});