2017-02-13 6 views
2

스위치가 동적으로 생성 되었기 때문에 '스위치'아이콘 색상을 Java 코드가 아닌 java 코드에서 변경하고 싶습니다. 최소 SDK는 16입니다. 도움을 주시면 감사하겠습니다.자바 코드에서 스위치 아이콘 색상 변경

Switch aSwitch = new Switch(context); 
    holder.llSwitch.addView(aSwitch); 
    holder.navIcon.setText(context.getResources().getString(R.string.fa_bell_o)); 
+1

봅니다 : 그것은 작동하지 않았다 @RajeshKushvaha – Blady214

+0

이 .setTintColor –

+0

시도 android.support.v7.widget.SwitchCompat. 오히려 그러한 속성은 없습니다. –

답변

2
당신은 버튼을 checkd입니다 확인 여부 및 상태와 색상을 설정할 수 있습니다

당신이 SwitchToggleButton 검사로이 답변 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); 
    } 
}); 
+0

그러나 이것은 전환 버튼이며 Switch를 사용해야합니다. –

+0

'isChecked()'funtion이 너무 늦어서 다시 입력 할 수 없습니다. http://www.mysamplecode.com/2013/04/android-switch-button-example.html –

+0

토글 색상을 변경하고 싶습니다. 기본 검정색 (isChecked()) to red –