http://my.oschina.net/huangsm/blog/40027

和大家分享一下关于android中PreferenceActivity使用以及为配置信息文件中添加图标的功能,首先给大家看一下效果图:

大家可以看到这是最终的效果图,android提供了很大的空间供开发者可以自行定义控件,你想让你的控件长成什么样子,你就可以让它长成什么样子。自己也很推崇这类开发思想,因为自行定义控件(前提:系统内置的控件满足不了自己的需求)的优点不言而喻。这边主要分享两种类型:1:单纯标题类型;2:带有复选框。

先和大家介绍一下PreferenceActivity的几种标签:系统提供的这些标签默认都是没有图片信息

Preference :没有图片单纯的文字

EditTextPreperence:带有编辑框

ListPreference:一个List集合,右边会有一个较小的向下的三角形

RingtonePreference :铃声的相关设置

其中上面的标签当中,都有android:key和android:title两个属性,如果你直接使系统内置的这些标签,其中没有任何的扩展,在通过SharedPreferences的时候对应的key便是android:key中的key,在你的应用中需要取值的时候也通过这个key来取,数据也不用开发者通过代码保存,系统自动就会保存您在设置中改变的数据。Android:key和android:title这两个属性需要在strings.xml中自行定义。

简单的介绍过后我们回过头来看看带有图片的PreferenceActivity是怎样实现。要实现带有图片的preference有两种方法;第一,使用系统内置的preference然后通过代码设置它的setLayoutResource(layoutResourceId),把layout的换成我们自己定义的layout,但是这有一种局限性就是如果有多preference个话,而且显示的图片也要不一样,那就意味着你有多少个preference就得有多少个自己定义的layout;另一种是通过继承preference来自己扩展,这样也叫灵活,不管你有多少个preference我只需要一个layout就可以搞定。

建立类为:IconOptionPreference.java继承Preference

重写构造函数,oncreateView(arg0),onBindView(arg0)这几个方法就可以,在oncreateView的代码如下:

protected View onCreateView(ViewGroup parent) {

    return LayoutInflater.from(getContext()).inflate(R.layout.app_item, parent, false);

}

你只需将自行定义的layout返回即可,接下来是绑定要显示的图片和要显示的值的问题,重写oncreateView方法,代码如下:

protected void onBindView(View view) {

super.onBindView(view);

ImageView icon = (ImageView) view.findViewById(R.id.item_image);

icon.setImageDrawable(mItemDrawable);

TextView title = (TextView) view.findViewById(R.id.item_title);

title.setText(getTitle());

}

构造函数中你只需将自己定义的属性,取出值即可。在android有结果过launcher开发的对自定义属性肯定不会陌生,代码如下:

public IconOptionPreference(Context context, AttributeSet attr){

super(context, attr);

TypedArray a = context.obtainStyledAttributes(attr, R.styleable.Preference);

int icon = a.getResourceId(R.styleable.Preference_image, 0);

mItemDrawable = context.getResources().getDrawable(icon);

a.recycle();

}

属性定义在这里就不描述了,不明白的可以留言。最后就是app_item.xml布局文件了,里面就是一个ImageView和TextView:

<?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="55dip"

android:orientation="horizontal">

<ImageViewandroid:id="@+id/item_image"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:paddingLeft="15dip"

android:layout_gravity="center_horizontal|center_vertical"/>

<TextView android:id="@+id/item_title"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:textSize="22dip"

android:paddingLeft="10dip"

android:layout_gravity="center_horizontal|center_vertical"

android:textColor="@color/preference_text_color"/>

</LinearLayout>

就这样完成了一个带有图片的preference,在使用是你和其他自定义标签是一样的使用如:

<cn.yunmai.cclauncher.IconOptionPreference

android:key="@string/start_mode_key"

android:title="@string/start_mode_title"

preference:image="@drawable/prefer_modelling"/>

preference:image即为自己定义的属性,这样就完成了一个带有标签的preference了。完整的代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/***
 * @author huangsm
 * @date 2012-2-1
 * @email huangsanm@gmail.com
 * @desc custom icon of preference
 */
public class IconOptionPreference extends Preference {
   private Drawable mItemDrawable;
   public IconOptionPreference(Context context) {
      super(context);
   }
 
   public IconOptionPreference(Context context, AttributeSet attr){
      super(context, attr);
      TypedArray a = context.obtainStyledAttributes(attr, R.styleable.Preference);
      int icon = a.getResourceId(R.styleable.Preference_image, 0);
      mItemDrawable = context.getResources().getDrawable(icon);
       a.recycle();
 
   }
   public IconOptionPreference(Context context, AttributeSet attr, int defStyle){
      super(context, attr, defStyle);
      TypedArray a = context.obtainStyledAttributes(attr, R.styleable.Preference, defStyle, 0);
      int icon = a.getResourceId(R.styleable.Preference_image, 0);
      mItemDrawable = context.getResources().getDrawable(icon);
      a.recycle();
   }
 
   @Override
   protected void onBindView(View view) {
      super.onBindView(view);
      ImageView icon = (ImageView) view.findViewById(R.id.item_image);
      icon.setImageDrawable(mItemDrawable);   
      TextView title = (TextView) view.findViewById(R.id.item_title);
      title.setText(getTitle());
   }
    @Override
   protected View onCreateView(ViewGroup parent) {
      return LayoutInflater.from(getContext()).inflate(R.layout.app_item, parent, false);
   }
}

第二种带有复选框的你只需将布局文件中加入复选框就可以了,但是有点需要注意的是,复选框默认会获取焦点,所以你在处理点击事件的时候你的点击事件怎么样也不会生效,就是因为checkbox把焦点给抢走了,checkbox.item.xml代码如下:

<CheckBox android:id="@+id/wallpaper_ismove"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:focusable="false"

android:clickable="false"

android:layout_centerVertical="true"

android:layout_alignParentRight="true"/>

就可以了,其中android:clickable=”false”这样设置是细节上的一个问题,你可以先去掉这个属性,当你测试的时候你会发现bug。完整代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/***
 * @author huangsm
 * @date 2012-2-2
 * @email huangsanm@gmail.com
 * @desc 扩展Checkboxpreference,加入icon
 */
public class IconCheckBoxPreference extends Preference {
 
    private Drawable mDrawable;
   private CheckBox mCheckBox;
   private Context mContext;
   //private View mView;
   public IconCheckBoxPreference(Context context) {
      super(context);
   }
   public IconCheckBoxPreference(Context context, AttributeSet attr) {      super(context, attr);
      this.mContext = context;
      TypedArray a = context.obtainStyledAttributes(attr, R.styleable.Preference);
      int icon = a.getResourceId(R.styleable.Preference_image, 0);
      mDrawable = context.getResources().getDrawable(icon);  
      a.recycle();
   }
 
   public IconCheckBoxPreference(Context context, AttributeSet attr, int defStyle) {
      super(context, attr, defStyle);
      this.mContext = context;
      TypedArray a = context.obtainStyledAttributes(attr, R.styleable.Preference);
      int icon = a.getResourceId(R.styleable.Preference_image, 0);
      mDrawable = context.getResources().getDrawable(icon);
      a.recycle();
   }
 
   @Override
   protected void onBindView(View view) {
      super.onBindView(view);
      //绑定自定义View实现回显
      ImageView i = (ImageView) view.findViewById(R.id.wallpaper_imageView);
      i.setImageDrawable(mDrawable);
      TextView t = (TextView) view.findViewById(R.id.wallpaper_title);
      t.setText(getTitle());  
      final SharedPreferences s = PreferenceManager.getDefaultSharedPreferences(mContext);
      mCheckBox = (CheckBox) view.findViewById(R.id.wallpaper_ismove);
      mCheckBox.setChecked(s.getBoolean(getKey(), true));
   }
   @Override
   protected View onCreateView(ViewGroup parent) {
      return LayoutInflater.from(getContext()).inflate(R.layout.checkbox_item, parent, false);
   }
 
   //这个方法是方便在点击事件的做处理
   public CheckBox getIconCheckbox(){
      return mCheckBox;
   }
}

自定义带有图片的PreferenceActivity的更多相关文章

  1. 自定义带图片和文字的ImageTextButton

    今天我们来讲一下有关自定义控件的问题,今天讲的这篇是从布局自定义开始的,难度不大,一看就明白,估计有的同学或者开发者看了说,这种方式多此一举,但是小编我不这么认为,多一种解决方式,就多一种举一反三的学 ...

  2. Android SeekBar自定义使用图片和颜色显示

    案例使用的图片如下:                            1.在res/drawable目录下新增一个xml风格文件,seekbar_define_style.xml ? 1 2 3 ...

  3. 雷林鹏分享:jQuery EasyUI 窗口 - 自定义带有工具条和按钮的对话框

    jQuery EasyUI 窗口 - 自定义带有工具条和按钮的对话框 您可以创建一个带有工具栏(toolbar)和按钮(button)的对话框(dialog),可以从 HTML 标记创建.这个教程描述 ...

  4. iOSUITableView头部带有图片并且下拉图片放大效果

    最近感觉UITableview头部带有图片,并且下拉时图片放大这种效果非常炫酷,所以动手实现了一下,效果如下图: 1.gif 实现原理很简单,就是在UITableview上边添加一个图片子视图,在ta ...

  5. 微信小程序自定义分享图片

    自定义分享图片 onShareAppMessage: (res) => { if (res.from === 'button') { console.log("来自页面内转发按钮&qu ...

  6. Android 自定义圆形图片 CircleImageView

    1.效果预览 1.1.布局中写自定义圆形图片的路径即可 1.2.然后看一看图片效果 1.3.原图是这样的 @mipmap/ic_launcher 2.使用过程 2.1.CircleImageView源 ...

  7. arcgis api 4.x for js 自定义叠加图片图层实现地图叠加图片展示(附源码下载)

    前言 关于本篇功能实现用到的 api 涉及类看不懂的,请参照 esri 官网的 arcgis api 4.x for js:esri 官网 api,里面详细的介绍 arcgis api 4.x 各个类 ...

  8. 在zabbix中实现发送带有图片的邮件和微信告警

    1 python实现在4.2版本zabbix发送带有图片的报警邮件 我们通常收到的报警,都是文字,是把动作中的消息内容当成了正文参数传给脚本,然后邮件或者微信进行接收,往往只能看到当前值,无法直观的获 ...

  9. H5页面在QQ和微信上分享,为什么不能自定义设置图片和摘要?

    [记录]title标签中的页面标题为抓取标题.body内第一个img标签内的图片为自动抓取缩略图,图片宽高要大于300,如果不希望显示出来,将标签宽高皆设置为0.摘要显示为来源链接,如需自定义需要通过 ...

随机推荐

  1. Spark快速数据处理

    原书名:Fast Data Processing with Spark 原出版社:Packt Publishing 作者: (美)Holden Karau 丛书名:大数据技术丛书 出版社:机械工业出版 ...

  2. [LeetCode#161] One Edit Distance

    Problem: Given two strings S and T, determine if they are both one edit distance apart. General Anal ...

  3. 总结@ 在C# 中的用法

    一 字符串中的用法 1.地球人都知道 C# 中 字符串常量可以以 @ 开头声名,这样的优点是转义序列“不”被处理,按“原样”输出,即我们不需要对转义字符加上 \ (反斜扛),就可以轻松coding.如 ...

  4. 浅析五大ASP.NET数据控件

    转自:http://kb.cnblogs.com/page/69207/ 摘要:ASP.NET中有不少的控件,在这当中有一部分是用来处理数据的控件.在这里我们正要讨论的就是ASP.NET数据控件,希望 ...

  5. (转载)AS3中的mouseEnabled与mouseChildren

    (转载)http://www.cnblogs.com/gongchen/archive/2013/05/09/3069055.html mouseEnabled与mouseChildren都是用来确定 ...

  6. yml文件数据的简洁表达方法(Hashes to OpenStruct)

    通过ruby编写测试脚本的时候,我还是喜欢采用yml来管理测试数据,就像以前的文章(Selenium WebDriver + Grid2 + RSpec之旅(五))提到的一样,但是在引用yml中的数据 ...

  7. c# const与readonly 关键字的比较

    C#中,const 与readonly是两个比较有用的关键字.const 与 readonly 定义的数据成员在初始化都不能再改变. 比如定义了 public class MathUtitlity   ...

  8. 2008---2009学年(A)B)第1学期《中国近代史纲要》课程考核试卷

    湖南人文科技学院公共课 2008---2009学年第1学期<中国近代史纲要>课程考核试卷(A) 考核方式: (闭卷)                                     ...

  9. 玩玩hibernate

    这几天师兄,让我玩玩hibernate,然后通过这个玩意写爬虫(spider).这一说不打紧,嗯,一个星期没有了,全都是由于配置环境,心很塞,整个星期的空闲时间都用来做重复的工作.在学习之前,我先查找 ...

  10. Windows不能在本地计算机启动OracleDBConsoleorcl .错误代码2

    Windows 不能在 本地计算机 启动 OracleDBConsoleorcl.有关更多信息,查阅系统事件日志.如果这是非 Microsoft 服务,请与服务厂商联系,并参考特定服务错误代码 2. ...