Android - 如何动态更改SwitchPreference交换机的拇指/轨道颜色

时间:2017-08-30 06:35:19

标签: android android-layout android-widget android-preferences switchpreference

到目前为止,我只能在文字上用setSpan()更改首选项的摘要/标题文字颜色,但我找不到任何可以改变Switch的颜色的内容。 SwitchPreference,我考虑了几个计划,包括设置自定义布局,或为偏好设置自定义适配器,但我还没有清楚知道如何实现它们,所以,你的帮助将会很大赞赏。

PS:我认为只是设置一个自定义布局,但在我的情况下不能工作,因为我需要在应用运行时更改SwitchPreference的颜色,并且用户可以设置自定义开关的颜色,所以主题在这种情况下也不会起作用。

1 个答案:

答案 0 :(得分:0)

好的,我自己找到了解决方案:

创建一个自定义的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更改了切换颜色,谢谢!)