自定义组件——Preference

Android SDK 提供好几个 Preference 组件,例如 CheckBoxPreference、EditTextPreference、DialogPreference、ListPreference 等,这些组件是跟 Android 提供的 Preference 存储机制绑定的,你可以通过这些组件来修改应用的一些配置。

但这些组件毕竟还不能满足100%的要求,假设我们需要为应用程序提供一个选择不同图片做为应用背景图的设置,我们需要一个很直观的就可以看到当前 所选择的图片,然后点击后可以浏览其他图片并选择。那么这些 Preference 就无法满足这个需求,因此我们需要对 Preference 进行扩展。


代码如下:

  1. import android.content.Context;  
  2. import android.content.Intent;  
  3. import android.os.Bundle;  
  4. import android.preference.Preference;  
  5. import android.preference.PreferenceActivity;  
  6. import android.util.AttributeSet;  
  7. import android.view.View;  
  8. import android.widget.ImageView;  
  9.   
  10. /** 
  11.  * 图片选项,用于设置图片和边框 
  12.  * @author Winter Lau 
  13.  */  
  14. public class ImageOptionPreference extends Preference {  
  15.   
  16.     private PreferenceActivity parent;  
  17.     private int mImage = R.drawable.car;  
  18.     private ImageView preview_img;  
  19.       
  20.     public ImageOptionPreference(Context context, AttributeSet attrs, int defStyle) {  
  21.         super(context, attrs, defStyle);  
  22.     }  
  23.   
  24.     public ImageOptionPreference(Context context, AttributeSet attrs) {  
  25.         super(context, attrs);  
  26.     }  
  27.   
  28.     public ImageOptionPreference(Context context) {  
  29.         super(context);  
  30.     }  
  31.       
  32.     void setActivity(PreferenceActivity parent) {  
  33.         this.parent = parent;  
  34.     }  
  35.       
  36.     @Override  
  37.     public boolean isPersistent() {  
  38.         return false;  
  39.     }  
  40.   
  41.     /** 
  42.      * 修改图片 
  43.      * @param newImage 
  44.      * @return 
  45.      */  
  46.     boolean ChangeGamePic(int newImage ){  
  47.         if(this.mImage == newImage)  
  48.             return false;  
  49.         GameGlobal.save_pic(newImage);  
  50.         this.mImage = newImage;  
  51.         preview_img.setImageResource(newImage);  
  52.         return true;  
  53.     }  
  54.   
  55.     @Override  
  56.     protected void onBindView(View view) {  
  57.         super.onBindView(view);  
  58.           
  59.         this.mImage = GameGlobal.get_pic();  
  60.         preview_img = (ImageView)view.findViewById(R.id.pref_current_img);  
  61.         preview_img.setImageResource(this.mImage);  
  62.     }     
  63.   
  64.     @Override  
  65.     protected void onClick() {  
  66.         super.onClick();  
  67.         Bundle bundle = new Bundle();  
  68.         bundle.putInt(GameGlobal.PREF_KEY_IMAGE, this.mImage);  
  69.         Intent intent = new Intent(parent, ImageSelector.class);  
  70.         intent.putExtras(bundle);  
  71.         parent.startActivityForResult(intent, MagicSetting.REQUEST_CODE_GAME_IMAGE);          
  72.     }  
  73.   
  74. }  
对应的 Perference 配置信息如下

<com.liusoft.android.fmagic.ImageOptionPreference        
        android:key="game_pic"
        android:persistent="false"
        android:title="@string/pref_pic_title"
        android:summary="@string/pref_pic_summary"
        android:widgetLayout="@layout/preference_widget_image"
    />

而 preference_widget_image 的信息如下

<?xml version="1.0" encoding="utf-8"?>

<!-- Layout used by ImageOptionPreference for the image option style. 
     This is inflated inside android.R.layout.preference. 
     -->
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pref_current_img"
    android:layout_width="54dip"
    android:layout_height="54dip"
    android:layout_marginRight="4dip"
    android:layout_gravity="center_vertical"
    android:focusable="false"
    android:clickable="false"
    android:background="#eeeeee"
    android:padding="2dip"
    />
而 这个 ImageView 的 Layout 就是在选项右边显示的图片。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值