美文网首页
Android Preference怎么用(未完)

Android Preference怎么用(未完)

作者: StephenLau | 来源:发表于2018-09-28 22:57 被阅读134次

    反思一下思考过程。

    背景

    在Android中Setting页如何做这个问题植根在脑中很久了,源于个人项目悬浮球中最核心的就是一个Setting页面来改变参数。项目的Setting页应该非常常见,但是当初搜了一下相关资料却发现问题很多,要达到我想要的效果,估计得费很大劲,项目中我还是选择了自己实现,大概思路也就是要自己监听所有UI的改变,保存用户改变的数据到SharedPreference中,根据设置值更新UI,还有根据需要监听某些设置的改变。

    现在有时间,重新整理一下实现Setting页面的一些资料。

    问题

    1. 有快速实现Android App 的Setting页的方法吗?
    2. Native的Preference、Support Library的Preference用哪个?
    3. 解决方案。

    Android new project中的Setting模板

    Android Studio中新建Activity中就有一个Setting模板。

    SettingsActivity extends AppCompatPreferenceActivity 同时使用PreferenceFragment作为跳转的设置页面。

    AppCompatPreferenceActivity(有 private AppCompatDelegate mDelegate;) extends PreferenceActivity 是PreferenceActivity使用AppCompatDelegate的兼容增强版。

    Google Setting的库--Preference

    当然得想到官方的实现,官方文档

    Preference的使用不难,简单来说就是用PreferenceFragment作为设置的主页面,在其中用addPreferencesFromResource方法加载一个xml文件,使用xml文件描述设置页面内有什么具体的设置项。一个个的设置项都是基于Preference类的,Preference类要提供在设置页面的View,并用SharedPreferences存储设置。

    但这个文档并没有提到v7 v14 Support Library中提供的Preference。

    Support Library

    support-library的功能介绍https://developer.android.google.cn/topic/libraries/support-library/features
    注意里面提到的v7 Preference Support Library和v14 Preference Support Library。

    稍微浏览了下源码,v7已有SwitchPreferenceCompat/PreferenceFragmentCompat,v14有SwitchPreference/PreferenceFragment,前者使用Compat类,后者没有,如分别使用android.support.v7.widget.SwitchCompat和android.widget.Switch,其他的代码逻辑几乎没有差别,见下面的compare图。

    image-20180923213223317

    混乱,用哪个好?

    HowTo use support.v7.preference with AppCompat and potential drawbacks 中有答案。

    主要的矛盾就是通常建议使用support library来获得最大的兼容性,但是v7.preference却存在着很多问题。

    Unfortunately, support.v7.preference has had everything related to dealing with Fragment stripped out, making it loose a lot of it's built-in functionality.
    Instead of just maintaining an XML, you now have to sub-class and override a lot of stuff, all of which is unfortunately undocumented.

    看到上面真是觉得感同身受。

    接近我想要的东西

    https://github.com/Gericop/Android-Support-Preference-V7-Fix ✨✨

    资料索引

    How to use the v7/v14 Preference Support library?

    v7v14Preference最基本用法。


    https://github.com/jakobulbrich/preferences-demo

    很全面的一系列教程。


    Building custom preferences with preference-v7

    Materialistic的代码作为例子,提到核心步骤是:

    set preferenceTheme in our theme in values/styles.xml, this is required. We can use the default @style/PreferenceThemeOverlay as value for a start

    extend android.support.v7.preference.Preference. This is the base class for all preference widgets

    inflate custom layout using setLayoutResource(int) or setWidgetLayoutResource(int) via constructor

    override onBindViewHolder(PreferenceViewHolder) with our view binding and click listener logic. We may need to disable the default click behavior if we want to click child view


    Creating custom preference

    android:id="@android:id/widget_frame, and then declare TextView as android:title and android:summary

    附加问题

    xml文件中的View的那些属性值从哪里可以查?

    未知🙈

    Android xml中的android、app、tools你真的了解吗

    xmlns:android="http://schemas.android.com/apk/res/android"

    • 首先我们要明白一点,xmlns是xml namespace的缩写,意思是xml命名空间。
    • 有了他,Android Studio就会在我们编写布局文件的时候给出提示,提示我们可以输入什么,不可以输入什么。也可以理解为语法文件,或者语法判断器。

    xmlns:app="http://schemas.android.com/apk/res-auto"

    • 在项目需求中,我们往往使用系统自带的属性以及控件是不够的,我们可能需要导入自定义控件的一些属性,或者support支持包之类的。
    • 现在的普遍做法是使用xmlns:app="http://schemas.android.com/apk/res-auto",因为res-auto可以引用所有的自定义包名。

    xmlns:tools="http://schemas.android.com/tools"

    tools可以告诉Android Studio,哪些属性在运行的时候是被忽略的,只在设计布局的时候有效。


    自定义attr Style styleable以及其应用

    相关文章

      网友评论

          本文标题:Android Preference怎么用(未完)

          本文链接:https://www.haomeiwen.com/subject/uksinftx.html