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

效果如图:

相关推荐
Embedded-Xin6 小时前
Linux架构优化——spdlog实现压缩及异步写日志
android·linux·服务器·c++·架构·嵌入式
萑澈7 小时前
更换SIM卡前必看:如何使用ADB导出并筛选指定SIM卡的全部短信
android
希侬7 小时前
Cursor生成APK加固签名脚本
android
urkay-7 小时前
Android 切换应用语言
android·java·kotlin·iphone·androidx
i***11867 小时前
MySQL-mysql zip安装包配置教程
android·mysql·adb
apihz8 小时前
批量获取3位未注册短域名免费API接口每日更新
android·服务器·网络·网络协议·tcp/ip
apihz8 小时前
域名注册状态查询免费API接口详细教程
android·服务器·网络·python·tcp/ip
节节虫8 小时前
GLSurfaceView原理深度剖析:从OpenGL ES到Android屏幕的渲染之旅
android
用户41659673693558 小时前
告别 XML ANR?Compose 的性能陷阱与重组优化实战指南
android
私人珍藏库8 小时前
[Android] 轻小说文库(1.23)
android·app·安卓·工具