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

效果如图:

相关推荐
为码消得人憔悴1 小时前
Android perfetto - 记录分析memory
android·性能优化
尤老师FPGA2 小时前
使用ZYNQ芯片和LVGL框架实现用户高刷新UI设计系列教程(第四十二讲)
android·java·ui
成都大菠萝2 小时前
2-2-29 快速掌握Kotlin-过滤函数filter
android
成都大菠萝2 小时前
2-2-18 快速掌握Kotlin-扩展属性
android
成都大菠萝2 小时前
2-2-21 快速掌握Kotlin-定义扩展文件
android
成都大菠萝2 小时前
2-2-19 快速掌握Kotlin-可空类型扩展函数
android
成都大菠萝2 小时前
2-2-23 快速掌握Kotlin-apply函数详解
android
2501_916007473 小时前
iOS 证书如何创建,从能生成到能长期使用
android·macos·ios·小程序·uni-app·cocoa·iphone
Just_Paranoid3 小时前
【AOSP】Android Dump 信息快速定位方法
android·adb·framework·service·aosp·dumpsys
帅得不敢出门3 小时前
MTK Android11获取真实wifi mac地址
android·mtk