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" />

效果如图:

相关推荐
kymjs张涛6 小时前
OpenClaw 学习小组:初识
android·linux·人工智能
范特西林9 小时前
实战演练——从零实现一个高性能 Binder 服务
android
范特西林10 小时前
代码的生成:AIDL 编译器与 Parcel 的序列化艺术
android
范特西林10 小时前
深入内核:Binder 驱动的内存管理与事务调度
android
范特西林11 小时前
解剖麻雀:Binder 通信的整体架构全景图
android
范特西林11 小时前
破冰之旅:为什么 Android 选择了 Binder?
android
奔跑中的蜗牛66612 小时前
一次播放器架构升级:Android 直播间 ANR 下降 60%
android
测试工坊14 小时前
Android 视频播放卡顿检测——帧率之外的第二战场
android
Kapaseker16 小时前
一杯美式深入理解 data class
android·kotlin
鹏多多16 小时前
Flutter使用screenshot进行截屏和截长图以及分享保存的全流程指南
android·前端·flutter