0

これまでのところ、テキスト上のsetSpan()で優先度の概要/タイトルのテキストの色を変更することはできますが、変更できるものは見つかりませんでしたスイッチの色はSwitchPreferenceで、私はカスタムレイアウトを設定することを含むいくつかの計画を考慮したり、好みのカスタムアダプタを設定しましたが、まだ達成する方法についての明確なアイデアはないので、 。Android - SwitchPreferenceのスイッチのサム/トラックの色を動的に変更する方法

PS:私は単純にカスタムレイアウトを設定しましたが、アプリが動作しているときにSwitchPreferenceの色を変更する必要があるため、私のケースでは機能しません。また、テーマもこの場合は機能しません。

+0

このhttps://stackoverflow.com/questions/21235829/custom-switchpreference-in-androidを参照してください。 – Gautam

+0

Gautam、私はすでにカスタムswitchpreferenceを試しましたが、問題は私のJavaコードでスイッチのサム/トラックの色を設定する方法がわかりません。 – jackz314

答えて

0

OK、私は自分で解決策を見つけた:

は、あなたが得たすべてのSwitchPreferenceウィジェットにそれを適用した後、カスタムSwitchPreferenceクラスを作成し、クラスは次のようになります。

基本的に
class ColorSwitchPreference extends SwitchPreference { 
    Switch aSwitch; 
    SharedPreferences sharedPreferences; 

    public ColorSwitchPreference(Context context){ 
     super(context); 
    } 

    public ColorSwitchPreference(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public ColorSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
    } 

    public ColorSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 
     super(context, attrs, defStyleAttr, defStyleRes); 
    } 


    @Override 
    protected void onBindView(View view) { 
     super.onBindView(view); 
     aSwitch = findSwitchInChildViews((ViewGroup) view); 
     if (aSwitch!=null) { 
      //do change color here 
      changeColor(aSwitch.isChecked(),aSwitch.isEnabled()); 
      aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
       @Override 
       public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
        changeColor(isChecked, aSwitch.isEnabled()); 
       } 
      }); 
     } 
    } 

    private void changeColor(boolean checked, boolean enabled){ 
     try { 
      sharedPreferences = getContext().getSharedPreferences("settings_data",MODE_PRIVATE); 
      //apply the colors here 
      int thumbCheckedColor = sharedPreferences.getInt("theme_color_key",Color.parseColor("#3F51B5")); 
      int thumbUncheckedColor = Color.parseColor("#ECECEC"); 
      int trackCheckedColor = sharedPreferences.getInt("theme_color_key",Color.parseColor("#3F51B5")); 
      int trackUncheckedColor = Color.parseColor("#B9B9B9"); 
      if(enabled){ 
       aSwitch.getThumbDrawable().setColorFilter(checked ? thumbCheckedColor : thumbUncheckedColor, PorterDuff.Mode.MULTIPLY); 
       aSwitch.getTrackDrawable().setColorFilter(checked ? trackCheckedColor : trackUncheckedColor, PorterDuff.Mode.MULTIPLY); 
      }else { 
       aSwitch.getThumbDrawable().setColorFilter(Color.parseColor("#B9B9B9"), PorterDuff.Mode.MULTIPLY); 
       aSwitch.getTrackDrawable().setColorFilter(Color.parseColor("#E9E9E9"), PorterDuff.Mode.MULTIPLY); 
      } 
     }catch (NullPointerException e){ 
      e.printStackTrace(); 
     } 
    } 

    private Switch findSwitchInChildViews(ViewGroup view) {// find the Switch widget in the SwitchPreference 
     for (int i=0;i<view.getChildCount();i++) { 
      View thisChildview = view.getChildAt(i); 
      if (thisChildview instanceof Switch) { 
       return (Switch)thisChildview; 
      } 
      else if (thisChildview instanceof ViewGroup) { 
       Switch theSwitch = findSwitchInChildViews((ViewGroup) thisChildview); 
       if (theSwitch!=null) return theSwitch; 
      } 
     } 
     return null; 
    } 
} 

findSwitchInChildViews()を使用して、SwitchPreferenceでSwitchウィジェットを取得し、そこから色を変更します。

これだけです!それは実際にはかなり簡単なアプローチですが、私はそれについて前に考えなかったので、この投稿が将来私の闘いを避けるために役立つことを願っています。

(PS:私はhereからスイッチの色を変え、hereからスイッチを見つけるためのコードを持って、ありがとう!)