PreferenceScreen 中如何自定义SwitchPreferenceCompat的布局
PreferenceScreen 中如何自定義SwitchPreferenceCompat的布局
Android Preference 使用請看這篇
Android Preference使用
系統(tǒng)設(shè)置的代碼:
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>默認(rèn)的系統(tǒng)設(shè)置頁面如下:
不符合要求,如何修改SwitchPreferenceCompat的布局屬性和SwitchCompat呢:
設(shè)置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的布局進(jìn)行點擊事件關(guān)聯(lián)
SwitchPreferenceCompat 中的onBindViewHolder方法也標(biāo)明了id
@Overridepublic void onBindViewHolder(PreferenceViewHolder holder) {super.onBindViewHolder(holder);View switchView = holder.findViewById(R.id.switchWidget);syncSwitchView(switchView);syncSummaryView(holder);}修改后的布局如下:
Preference相關(guān)聯(lián)的xml文件如下:preference.xml,preference_material.xml
參考文章:
Android 修改Preferences默認(rèn)樣式的步驟
Preference的 簡單講解
總結(jié)
以上是生活随笔為你收集整理的PreferenceScreen 中如何自定义SwitchPreferenceCompat的布局的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 网上书店系统mysql设计_数据库设计-
- 下一篇: 人口各省预测模型matlab_MATLA