Android 自定义SwitchPreference

  1. 为SwitchPreference 添加背景:custom_preference_background.xml
XML 复制代码
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle" >
            <gradient android:startColor="@color/white" android:endColor="@color/white" android:angle="90"/>
            <corners android:radius="8dp"/>
        </shape>
    </item>
</selector>
  1. 自定义 CustomSwitchPreference 继承自 witchPreference
java 复制代码
public class CustomSwitchPreference extends SwitchPreference {

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

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

//
....
//
}
  1. 重载 onBindViewHolder, 实现自定义的效果, 如:设置背景、添加外边距、设置内边距、修改文字的颜色和字体等等:
java 复制代码
    @Override
    public void onBindViewHolder(PreferenceViewHolder holder) {
        super.onBindViewHolder(holder);
    //设置背景

     
    
   holder.itemView.setBackgroundResource(R.drawable.custom_preference_background);
        
    //设置内边距
    holder.itemView.setPadding(
                DensityUtils.dp2px(getContext(),20),
                DensityUtils.dp2px(getContext(),2),
                DensityUtils.dp2px(getContext(),20),
                DensityUtils.dp2px(getContext(),2));

          //设置外边距
        ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams)   holder.itemView.getLayoutParams();
        int textSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, 18, getContext().getResources().getDisplayMetrics());
        params.topMargin = DensityUtils.dp2px(getContext(),5);
        params.bottomMargin = DensityUtils.dp2px(getContext(),5);
        params.leftMargin = DensityUtils.dp2px(getContext(),20);
        params.rightMargin = DensityUtils.dp2px(getContext(),20);
        holder.itemView.setLayoutParams(params);

        TextView titleView = holder.itemView.findViewById(android.R.id.title);
        titleView.setTextColor(Color.BLACK); // 这里设置为红色,你可以根据需要设置其他颜色
        TextView summaryView = holder.itemView.findViewById(android.R.id.summary);
        summaryView.setTextColor(Color.BLACK); // 设置为黑色
    }

完整代码如下:

java 复制代码
public class CustomSwitchPreference extends SwitchPreference {

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

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

    @Override
    public void onBindViewHolder(PreferenceViewHolder holder) {
        super.onBindViewHolder(holder);
        holder.itemView.setBackgroundResource(R.drawable.custom_preference_background);
        holder.itemView.setPadding(
                DensityUtils.dp2px(getContext(),20),
                DensityUtils.dp2px(getContext(),2),
                DensityUtils.dp2px(getContext(),20),
                DensityUtils.dp2px(getContext(),2));

        ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams)   holder.itemView.getLayoutParams();
        int textSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, 18, getContext().getResources().getDisplayMetrics());
        params.topMargin = DensityUtils.dp2px(getContext(),5);
        params.bottomMargin = DensityUtils.dp2px(getContext(),5);
        params.leftMargin = DensityUtils.dp2px(getContext(),20);
        params.rightMargin = DensityUtils.dp2px(getContext(),20);
        holder.itemView.setLayoutParams(params);

        TextView titleView = holder.itemView.findViewById(android.R.id.title);
        titleView.setTextColor(Color.BLACK); // 这里设置为红色,你可以根据需要设置其他颜色
        TextView summaryView = holder.itemView.findViewById(android.R.id.summary);
        summaryView.setTextColor(Color.BLACK); // 设置为蓝色
    }
}

用法:

java 复制代码
    <com.tetras.sensechat.wedgit.CustomSwitchPreference
        app:defaultValue="true"
        app:iconSpaceReserved="false"
        app:key="key_continuous_switch"
        app:title="@string/text_continuous_switch" />

效果如图:

相关推荐
Mr_sun.3 小时前
Day09——入退管理-入住-2
android·java·开发语言
ujainu4 小时前
告别杂乱!Flutter + OpenHarmony 鸿蒙记事本的标签与分类管理(三)
android·flutter·openharmony
常利兵5 小时前
Android内存泄漏:成因剖析与高效排查实战指南
android
·云扬·5 小时前
MySQL 8.0 Redo Log 归档与禁用实战指南
android·数据库·mysql
野生技术架构师5 小时前
SQL语句性能优化分析及解决方案
android·sql·性能优化
doupoa6 小时前
内存指针是什么?为什么指针还要有偏移量?
android·c++
非凡ghost7 小时前
PowerDirector安卓版(威力导演安卓版)
android·windows·学习·软件需求
独行soc7 小时前
2026年渗透测试面试题总结-19(题目+回答)
android·网络·安全·web安全·渗透测试·安全狮
爱装代码的小瓶子9 小时前
【C++与Linux基础】进程间通讯方式:匿名管道
android·c++·后端
兴趣使然HX9 小时前
Android绘帧流程解析
android