自定義帶有圖片的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;
   }
}
相關文章
相關標籤/搜索