당신은 버튼을 checkd입니다 확인 여부 및 상태와 색상을 설정할 수 있습니다
당신이 Switch
및 ToggleButton
검사로이 답변 Switch vs toggle
를 엉망으로하고 있기 때문에
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linear_layout);
Switch mySwitch = new Switch(this);
linearLayout.addView(mySwitch);
mySwitch.setBackgroundColor(Color.BLACK);
mySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked)
buttonView.setBackgroundColor(Color.RED);
else buttonView.setBackgroundColor(Color.BLACK);
}
});
편집 : 엄지 색 변경 만 시도하면 아래처럼 시도 할 수 있습니다
사용
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linear_layout);
final Switch mySwitch = new Switch(this);
linearLayout.addView(mySwitch);
mySwitch.getThumbDrawable().setColorFilter(Color.RED, PorterDuff.Mode.MULTIPLY);
mySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked)
mySwitch.getThumbDrawable().setColorFilter(Color.RED, PorterDuff.Mode.MULTIPLY);
else
mySwitch.getThumbDrawable().setColorFilter(Color.BLACK, PorterDuff.Mode.MULTIPLY);
}
});
봅니다 : 그것은 작동하지 않았다 @RajeshKushvaha – Blady214
이 .setTintColor –
시도 android.support.v7.widget.SwitchCompat. 오히려 그러한 속성은 없습니다. –