Jack Frost

Android-自定义preference和PreferenceFragment实例详解

我们在Android原生系统中所看到的设置界面,使用的是与Preference相关的视图完成的。使用Preference相关的视图能较好地进行了文字的分类和排版。而原生的preference基本很难满足我们的需求开发,所以自定义preference必不可缺。而为了使应用更轻量更碎片化,preferencefragment也是需要的。


本篇文章结构将围绕一个例子进行详解,同时使用了自定义preference和preferencefragment。1.先讲解选择开关的自定义preference(数据存储在这里细讲) 2.结合本图例子讲解preferencefragment的使用

先上图这里写图片描述 可以看到,这是仿谷歌样式的自定义preferenc结合preferencefragment的体现。本博文将重点讲解最难的图中第二项部分。相信懂得第二项(启动语音输出)的制作,大家都会掌握自定义preference的使用啦!!此外本文也将讲解选择开关的制作(附带的!)


1.当然是该栏的布局啦,item_preference_voice.xml

2.还有那个选择开关的样式,就是利用selector来决定选中与非选中啦


3.就是自定义preference的文件啦:

4.自定义preference的属性:

5.就是最后的调用啦,这个就是我的preferencefragment的布局文件啦

6.然后给出最终的在preferencefragment的调用:

如果您是开发针对 Android 3.0(API 级别 11)及更高版本系统的应用,则应使用 PreferenceFragment 显示 Preference 对象的列表。您可以将 PreferenceFragment 添加到任何 Activity,而不必使用 PreferenceActivity。

无论您在构建何种 Activity,片段都可为应用提供一个更加灵活的体系结构。 因此,我们建议您尽可能使用 PreferenceFragment 控制设置的显示,而不是使用 PreferenceActivity。 PreferenceFragment 的实现就像定义 onCreate() 方法以使用 addPreferencesFromResource() 加载首选项文件一样简单。

7.preference_list.xml

### 8.补充刚刚遗漏的自定义PreferenceView


好啦,到此就讲完自定义preference和PreferenceFragment的使用啦。最后给出一篇基本应用preference的文章,还没学过用preference的同学可以看下。基本的preference应用

欢迎指出错误,共同学习!啦啦啦啦!

转载请注明:【JackFrost的博客】

码字很辛苦,转载请注明来自JackFrost《Android-自定义preference和PreferenceFragment实例详解》

7 responses to “Android-自定义preference和PreferenceFragment实例详解”

  1. Michael says:

    不错呀,挺详细的

  2. Excellent article. I certainly appreciate this site.

    Thanks!

  3. Hi there to every body, it’s my first visit
    of this blog; this website consists of awesome and in fact
    excellent material designed for readers.

  4. I seriously love your blog.. Very nice colors & theme.
    Did you create this site yourself? Please reply back
    as I’m looking to create my own personal website
    and would like to learn where you got this from or what the theme is named.
    Many thanks!

  5. I read this article fully concerning the difference of latest
    and earlier technologies, it’s remarkable article.

  6. Its like you read my mind! You appear to know a lot about this, like
    you wrote the book in it or something. I think that you could
    do with a few pics to drive the message home a little bit, but instead of that, this is magnificent blog.
    An excellent read. I’ll certainly be back.

  7. My brother recommended I may like this web site. He used to be entirely right.
    This put up actually made my day. You cann’t believe just how
    a lot time I had spent for this information! Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *