效果图:
布局文件:
<PreferenceScreenxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"><PreferenceCategoryandroid:title="@string/basic_setting"><CheckBoxPreferenceandroid:defaultValue="false"android:key="switch_noPhotoMode"android:summary="@string/switch_no_photo_mode_summary"android:title="@string/switch_photo_mode"/><Preferenceandroid:key="auto_nightMode"android:summary="@string/auto_night_mode_summary"android:title="@string/auto_night_mode"/><PreferenceCategoryandroid:title="@string/video_setting"><CheckBoxPreferenceandroid:defaultValue="false"android:key="video_force_landscape"android:summary="@string/video_force_landscape_summary"android:title="@string/video_force_landscape"/><CheckBoxPreferenceandroid:defaultValue="false"android:key="video_auto_play"android:summary="@string/video_auto_play_summary"android:title="@string/video_auto_play"/></PreferenceCategory><Preferenceandroid:key="text_size"android:summary="设置字体大小"android:title="字体大小"/></PreferenceCategory><PreferenceCategoryandroid:title="@string/other_setting"><com.meiji.toutiao.widget.IconListPreferenceandroid:defaultValue="0"android:entries="@array/icons"android:entryValues="@array/iconsValues"android:key="custom_icon"android:summary="@string/choose_icon_summary"android:title="@string/choose_icon"app:iconsDrawables="@array/iconsDrawables"/><com.meiji.toutiao.widget.IconPreferenceandroid:key="color"android:summary="@string/choose_theme_color_summary"android:title="@string/choose_theme_color"/><ListPreferenceandroid:defaultValue="1"android:entries="@array/slidable"android:entryValues="@array/slidableValues"android:key="slidable"android:negativeButtonText="@string/cancel"android:summary="@string/slidable_summary"android:title="@string/slidable_title"/><CheckBoxPreferenceandroid:defaultValue="false"android:key="nav_bar"android:summary="@string/nav_bar_coloration_summary"android:title="@string/nav_bar_coloration"/><Preferenceandroid:key="clearCache"android:title="@string/clear_cache"/></PreferenceCategory><PreferenceCategoryandroid:title="@string/about_settings"><Preferenceandroid:key="version"android:title="@string/version"/><Preferenceandroid:key="changelog"android:summary="@string/changelog_url"android:title="@string/changelog"/><Preferenceandroid:key="licenses"android:summary="@string/about_libraries_summary"android:title="@string/about_libraries_label"/><Preferenceandroid:key="sourceCode"android:summary="@string/source_code_url"android:title="@string/source_code"/><Preferenceandroid:key="copyRight"android:summary="@string/copyright_summary"android:title="@string/copyright"/></PreferenceCategory></PreferenceScreen>
Java代码:
public class GeneralPreferenceFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {private IconPreference colorPreview;private SettingActivity context;public static GeneralPreferenceFragment newInstance() {return new GeneralPreferenceFragment();}@Overridepublic void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);addPreferencesFromResource(R.xml.pref_general);context = (SettingActivity) getActivity();setHasOptionsMenu(true);setText();findPreference("auto_nightMode").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {@Overridepublic boolean onPreferenceClick(Preference preference) {context.startWithFragment(AutoNightModeFragment.class.getName(), null, null, 0, null);return true;}});findPreference("text_size").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {@Overridepublic boolean onPreferenceClick(Preference preference) {context.startWithFragment(TextSizeFragment.class.getName(), null, null, 0, null);return true;}});findPreference("custom_icon").setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {@Overridepublic boolean onPreferenceChange(Preference preference, Object newValue) {int selectValue = Integer.parseInt((String) newValue);int drawable = Constant.ICONS_DRAWABLES[selectValue];if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {ActivityManager.TaskDescription tDesc = new ActivityManager.TaskDescription(getString(R.string.app_name),BitmapFactory.decodeResource(getResources(), drawable),SettingUtil.getInstance().getColor());context.setTaskDescription(tDesc);}return true;}});findPreference("color").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {@Overridepublic boolean onPreferenceClick(Preference preference) {new ColorChooserDialog.Builder(context, R.string.choose_theme_color).backButton(R.string.back).cancelButton(R.string.cancel).doneButton(R.string.done).customButton(R.string.custom).presetsButton(R.string.back).allowUserColorInputAlpha(false).show();return false;}});colorPreview = (IconPreference) findPreference("color");findPreference("nav_bar").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {@Overridepublic boolean onPreferenceClick(Preference preference) {int color = SettingUtil.getInstance().getColor();if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {if (SettingUtil.getInstance().getNavBar()) {context.getWindow().setNavigationBarColor(CircleView.shiftColorDown(CircleView.shiftColorDown(color)));} else {context.getWindow().setNavigationBarColor(Color.BLACK);}}return false;}});findPreference("clearCache").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {@Overridepublic boolean onPreferenceClick(Preference preference) {CacheDataManager.clearAllCache(context);Snackbar.make(getView(), R.string.clear_cache_successfully, Snackbar.LENGTH_SHORT).show();setText();return false;}});try {String version = "当前版本 " + context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;findPreference("version").setSummary(version);} catch (PackageManager.NameNotFoundException e) {e.printStackTrace();}findPreference("changelog").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {@Overridepublic boolean onPreferenceClick(Preference preference) {context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.changelog_url))));return false;}});findPreference("licenses").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {@Overridepublic boolean onPreferenceClick(Preference preference) {createLicenseDialog();return false;}});findPreference("sourceCode").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {@Overridepublic boolean onPreferenceClick(Preference preference) {context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.source_code_url))));return false;}});findPreference("copyRight").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {@Overridepublic boolean onPreferenceClick(Preference preference) {new AlertDialog.Builder(context).setTitle(R.string.copyright).setMessage(R.string.copyright_content).setCancelable(true).show();return false;}});}private void setText() {try {findPreference("clearCache").setSummary(CacheDataManager.getTotalCacheSize(context));} catch (Exception e) {e.printStackTrace();}}private void createLicenseDialog() {Notices notices = new Notices();notices.addNotice(new Notice("PhotoView", "https://github.com/chrisbanes/PhotoView", "Copyright 2017 Chris Banes", new ApacheSoftwareLicense20()));notices.addNotice(new Notice("OkHttp", "https://github.com/square/okhttp", "Copyright 2016 Square, Inc.", new ApacheSoftwareLicense20()));notices.addNotice(new Notice("Gson", "https://github.com/google/gson", "Copyright 2008 Google Inc.", new ApacheSoftwareLicense20()));notices.addNotice(new Notice("Glide", "https://github.com/bumptech/glide", "Sam Judd - @sjudd on GitHub, @samajudd on Twitter", new ApacheSoftwareLicense20()));notices.addNotice(new Notice("Stetho", "https://github.com/facebook/stetho", "Copyright (c) 2015, Facebook, Inc. All rights reserved.", new BSD3ClauseLicense()));notices.addNotice(new Notice("PersistentCookieJar", "https://github.com/franmontiel/PersistentCookieJar", "Copyright 2016 Francisco José Montiel Navarro", new ApacheSoftwareLicense20()));notices.addNotice(new Notice("jsoup", "https://jsoup.org", "Copyright © 2009 - 2016 Jonathan Hedley (jonathan@hedley.net)", new MITLicense()));new LicensesDialog.Builder(context).setNotices(notices).setIncludeOwnLicense(true).build().show();}@Overridepublic void onResume() {super.onResume();getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);}@Overridepublic void onPause() {super.onPause();getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);}@Overridepublic void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {if (key.equals("color")) {colorPreview.setView();}if (key.equals("slidable")) {context.recreate();}}
}









