[SwitchPreference]代码中动态修改SwitchPreference的Thumb或Track颜色
Posted peak wang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[SwitchPreference]代码中动态修改SwitchPreference的Thumb或Track颜色相关的知识,希望对你有一定的参考价值。
所有自定义代码在下面▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼
逻辑就是自定义个class继承SwitchPreference,拿到switch,给switch设置checkChange监听就可以了;
解释下为何holder.findViewById(16908352)中id是16908352呢?因为我们是继承自support-v14中的SwitchPreference,(注意:不是android.preference.SwitchPreference,这个已经被弃用了),查看SwitchPreference中onBindViewHoldr的代码,它的Switch是局部变量,所以我们是无法继承的,但已知switch的id是16908352,所以我们直接拿过来用就可以了,不用纠结它的id常量名是什么.
package com.android.MySettings.widget;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.support.v14.preference.SwitchPreference;
import android.support.v7.preference.PreferenceViewHolder;
import android.util.AttributeSet;
import android.widget.CompoundButton;
import android.widget.Switch;
/**
* When I wrote this,only God and I understood what I was doing.
* Now, God only knows.
*
* @fileName:MySwitchPreference
* @author:86132
* @date:2022/12/15 17:56
*/
public class MySwitchPreference extends SwitchPreference
private Switch mSwitch;
private SharedPreferences mSharedPreferences;
public MySwitchPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)
super(context, attrs, defStyleAttr, defStyleRes);
public MySwitchPreference(Context context, AttributeSet attrs, int defStyleAttr)
super(context, attrs, defStyleAttr);
public MySwitchPreference(Context context, AttributeSet attrs)
super(context, attrs);
public MySwitchPreference(Context context)
super(context);
@SuppressLint("ResourceType")
@Override
public void onBindViewHolder(PreferenceViewHolder holder)
super.onBindViewHolder(holder);
mSwitch = (Switch) holder.findViewById(16908352);
if(null != mSwitch)
changeColor(mSwitch.isChecked(),mSwitch.isEnabled());
mSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
changeColor(isChecked,mSwitch.isEnabled());
);
private void changeColor(boolean checked, boolean enabled)
try
mSharedPreferences = getContext().getSharedPreferences("settings_data",Context.MODE_PRIVATE);
int thumbCheckedColor = Color.parseColor("#33ff99");
int thumbUncheckedColor = Color.parseColor("#ECECEC");
/* int trackCheckedColor = mSharedPreferences.getInt("theme_color_key",Color.parseColor("#3F51B5"));
int trackUncheckedColor = Color.parseColor("#B9B9B9");*/
if(enabled)
mSwitch.getThumbDrawable().setColorFilter(checked ? thumbCheckedColor : thumbUncheckedColor, PorterDuff.Mode.MULTIPLY);
// mSwitch.getTrackDrawable().setColorFilter(checked ? trackCheckedColor : trackUncheckedColor, PorterDuff.Mode.MULTIPLY);
else
mSwitch.getThumbDrawable().setColorFilter(Color.parseColor("#B9B9B9"), PorterDuff.Mode.MULTIPLY);
// mSwitch.getTrackDrawable().setColorFilter(Color.parseColor("#E9E9E9"), PorterDuff.Mode.MULTIPLY);
catch (NullPointerException e)
e.printStackTrace();
以上是关于[SwitchPreference]代码中动态修改SwitchPreference的Thumb或Track颜色的主要内容,如果未能解决你的问题,请参考以下文章
Android - 如何动态更改 SwitchPreference 开关的拇指/轨道颜色
在 Android Lollipop 中扩展 Preference 类 = 丢失动画