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"--><ImageViewandroid:id="@android:id/icon"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginStart="@dimen/dp_20"android:layout_gravity="center" /><RelativeLayoutandroid: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"><TextViewandroid: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"/><TextViewandroid: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.SwitchCompatandroid: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
@Overridepublic 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的 简单讲解

















