PreferenceScreen 中如何自定义SwitchPreferenceCompat的布局

PreferenceScreen 中如何自定义SwitchPreferenceCompat的布局

Android Preference 使用请看这篇

Android Preference使用

系统设置的代码:

public class SetActivity extends AppCompatActivity {
@Override  
protected void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.settings_activity);  
    if (savedInstanceState == null) {  
        getSupportFragmentManager()  
                .beginTransaction()  
                .replace(R.id.settings, new SettingsFragment())  
                .commit();  
    }  
    ActionBar actionBar = getSupportActionBar();  
    if (actionBar != null) {  
        actionBar.setDisplayHomeAsUpEnabled(true);  
    }  
}  

public static class SettingsFragment extends PreferenceFragmentCompat {  
    @Override  
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {  
        setPreferencesFromResource(R.xml.set, rootKey);  
    }  
}  

set.xml 如下:

  <PreferenceScreen  
 xmlns:android="http://schemas.android.com/apk/res/android"  
 xmlns:app="http://schemas.android.com/apk/res-auto">>  
 <PreferenceCategory app:title="@string/messages_header">  
  <EditTextPreference            
       app:key="signature"  
        app:title="@string/signature_title"  
        app:useSimpleSummaryProvider="true" />  

    <ListPreference            app:defaultValue="reply"  
        app:entries="@array/reply_entries"  
        app:entryValues="@array/reply_values"  
        app:key="reply"  
        app:title="@string/reply_title"  
        app:useSimpleSummaryProvider="true" />  

</PreferenceCategory>   
   <PreferenceCategory app:title="@string/sync_header">  

    <SwitchPreferenceCompat           
        app:key="sync"  
        app:title="@string/sync_title" />  

    <SwitchPreferenceCompat            
        app:dependency="sync"  
        app:key="attachment"  
        app:summaryOff="@string/attachment_summary_off"  
        app:summaryOn="@string/attachment_summary_on"  
        app:title="@string/attachment_title" />  

</PreferenceCategory>
 </PreferenceScreen>

默认的系统设置页面如下:

在这里插入图片描述

不符合要求,如何修改SwitchPreferenceCompat的布局属性和SwitchCompat呢:

设置layout 布局

<SwitchPreferenceCompat  
    app:key="sync"  
    app:title="@string/sync_title"
    app:layout="@layout/layout_setting_item"  
   />

layout_setting_item

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:gravity="center_vertical"
    android:background="@null"
    android:minHeight="@dimen/dp_40">
    <!--   style="?android:attr/borderlessButtonStyle"-->

    <ImageView
        android:id="@android:id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/dp_20"
        android:layout_gravity="center" />

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dip"
        android:layout_marginTop="6dip"
        android:layout_marginEnd="6dip"
        android:layout_marginBottom="6dip"
        android:layout_weight="1">

        <TextView
            android:id="@android:id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="marquee"
            android:fadingEdge="horizontal"
            android:singleLine="true"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="@color/black_222"
            android:textSize="@dimen/sp_15"/>

        <TextView
            android:id="@android:id/summary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@android:id/title"
            android:layout_alignStart="@android:id/title"
            android:maxLines="4"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="?android:attr/textColorSecondary" />

    </RelativeLayout>

    <androidx.appcompat.widget.SwitchCompat
        android:id="@+id/switchWidget"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@null"
        android:translationX="@dimen/dp_12"
        android:checked="true"
        android:thumb="@drawable/switdth="wrap_content"
        android:layout_height="wrap_content"/>-->
</LinearLayout>

这样就可以替换原有的布局属性,id 需要声明为@android:id/

看到SwitchCompat 用的@+id/switchWidget ,为啥不用@android:id/switch_widget 或者@+id/switch_widget,

有2点原因:1.@android:id/switch_widget 在android7.0以上才能使用 2.用了以后无法和该item的布局进行点击事件关联

SwitchPreferenceCompat 中的onBindViewHolder方法也标明了id

    @Override
    public void onBindViewHolder(PreferenceViewHolder holder) {
        super.onBindViewHolder(holder);
        View switchView = holder.findViewById(R.id.switchWidget);
        syncSwitchView(switchView);
        syncSummaryView(holder);
    }

修改后的布局如下:
在这里插入图片描述
Preference相关联的xml文件如下:preference.xml,preference_material.xml

参考文章:

Android 修改Preferences默认样式的步骤

Preference的 简单讲解

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要自定义 SwitchPreferenceCompat,您需要创建一个新的类扩展 SwitchPreferenceCompat,并覆盖其的一些方法。 以下是一个示例: ```java public class CustomSwitchPreferenceCompat extends SwitchPreferenceCompat { public CustomSwitchPreferenceCompat(Context context) { super(context); } public CustomSwitchPreferenceCompat(Context context, AttributeSet attrs) { super(context, attrs); } public CustomSwitchPreferenceCompat(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override public void onBindViewHolder(PreferenceViewHolder holder) { super.onBindViewHolder(holder); // 获取 SwitchCompat 组件 SwitchCompat switchCompat = (SwitchCompat) holder.findViewById(R.id.switchWidget); // 更改 SwitchCompat 组件的样式和颜色 switchCompat.setThumbTintList(ContextCompat.getColorStateList(getContext(), R.color.switch_thumb_color)); switchCompat.setTrackTintList(ContextCompat.getColorStateList(getContext(), R.color.switch_track_color)); switchCompat.setTextOn("ON"); switchCompat.setTextOff("OFF"); } } ``` 在这个示例,我们创建了一个名为 `CustomSwitchPreferenceCompat` 的新类,继承了 SwitchPreferenceCompat 类。我们覆盖了 `onBindViewHolder` 方法,并在其获取了 SwitchCompat 组件,并更改了其样式和颜色。 接下来,在您的布局文件使用自定义 SwitchPreferenceCompat: ```xml <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <com.example.CustomSwitchPreferenceCompat android:key="example_switch" android:title="Switch" android:summary="This is a switch preference" /> </PreferenceScreen> ``` 在这个例子,我们使用了自定义 SwitchPreferenceCompat,并将其键设置为 `example_switch`,标题设置为 `Switch`,摘要设置为 `This is a switch preference`。 您可以根据需要更改自定义 SwitchPreferenceCompat 的样式和颜色,并添加其他自定义行为。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值