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からスイッチを見つけるためのコードを持って、ありがとう!)
このhttps://stackoverflow.com/questions/21235829/custom-switchpreference-in-androidを参照してください。 – Gautam
Gautam、私はすでにカスタムswitchpreferenceを試しましたが、問題は私のJavaコードでスイッチのサム/トラックの色を設定する方法がわかりません。 – jackz314