自定义带有图片的PreferenceActivity

次和你们分享一下关于androidPreferenceActivity使用以及为配置信息文件中添加图标的功能,首先给你们看一下效果图:java


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

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

Preference :没有图片单纯的文字ide

EditTextPreperence:带有编辑框函数

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

RingtonePreference :铃声的相关设置测试

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

简单的介绍事后咱们回过头来看看带有图片的PreferenceActivity是怎样实现。要实现带有图片的preference有两种方法;第一,使用系统内置的preference而后经过代码设置它的setLayoutResource(layoutResourceId),把layout的换成咱们本身定义的layout,可是这有一种局限性就是若是有多preference个话,并且显示的图片也要不同,那就意味着你有多少个preference就得有多少个本身定义的layout;另外一种是经过继承preference来本身扩展,这样也叫灵活,无论你有多少个preference我只须要一个layout就能够搞定。spa

创建类为:IconOptionPreference.java继承Preferencecode

重写构造函数,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布局文件了,里面就是一个ImageViewTextView

<?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">   

    <ImageView android: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了。完整的代码以下:

/***
 * @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。完整代码以下:

/***
 * @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;
   }
}
相关文章
相关标签/搜索