Android SDK 提供好几个 Preference 组件,例如 CheckBoxPreference、EditTextPreference、DialogPreference、ListPreference 等,这些组件是跟 Android 提供的 Preference 存储机制绑定的,你可以通过这些组件来修改应用的一些配置,如下图所示,这是 Android 自带的系统设置界面:

但这些组件毕竟还不能满足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. *@authorWinterLau
  12. */
  13. public class ImageOptionPreference extends Preference{
  14. private PreferenceActivityparent;
  15. private int mImage=R.drawable.car;
  16. private ImageViewpreview_img;
  17. public ImageOptionPreference(Contextcontext,AttributeSetattrs, int defStyle){
  18. super (context,attrs,defStyle);
  19. }
  20. public ImageOptionPreference(Contextcontext,AttributeSetattrs){
  21. super (context,attrs);
  22. }
  23. public ImageOptionPreference(Contextcontext){
  24. super (context);
  25. }
  26. void setActivity(PreferenceActivityparent){
  27. this .parent=parent;
  28. }
  29. @Override
  30. public boolean isPersistent(){
  31. return false ;
  32. }
  33. /**
  34. *修改图片
  35. *@paramnewImage
  36. *@return
  37. */
  38. boolean ChangeGamePic( int newImage){
  39. if ( this .mImage==newImage)
  40. return false ;
  41. GameGlobal.save_pic(newImage);
  42. this .mImage=newImage;
  43. preview_img.setImageResource(newImage);
  44. return true ;
  45. }
  46. @Override
  47. protected void onBindView(Viewview){
  48. super .onBindView(view);
  49. this .mImage=GameGlobal.get_pic();
  50. preview_img=(ImageView)view.findViewById(R.id.pref_current_img);
  51. preview_img.setImageResource(this .mImage);
  52. }
  53. @Override
  54. protected void onClick(){
  55. super .onClick();
  56. Bundlebundle=new Bundle();
  57. bundle.putInt(GameGlobal.PREF_KEY_IMAGE,this .mImage);
  58. Intentintent=new Intent(parent,ImageSelector. class );
  59. intent.putExtras(bundle);
  60. parent.startActivityForResult(intent,MagicSetting.REQUEST_CODE_GAME_IMAGE);
  61. }
  62. }
import android.content.Context;import android.content.Intent;import android.os.Bundle;import android.preference.Preference;import android.preference.PreferenceActivity;import android.util.AttributeSet;import android.view.View;import android.widget.ImageView;/** * 图片选项,用于设置图片和边框 * @author Winter Lau */public class ImageOptionPreference extends Preference {private PreferenceActivity parent;private int mImage = R.drawable.car;private ImageView preview_img;public ImageOptionPreference(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);}public ImageOptionPreference(Context context, AttributeSet attrs) {super(context, attrs);}public ImageOptionPreference(Context context) {super(context);}void setActivity(PreferenceActivity parent) {this.parent = parent;}@Overridepublic boolean isPersistent() {return false;}/** * 修改图片 * @param newImage * @return */boolean ChangeGamePic(int newImage ){if(this.mImage == newImage)return false;GameGlobal.save_pic(newImage);this.mImage = newImage;preview_img.setImageResource(newImage);return true;}@Overrideprotected void onBindView(View view) {super.onBindView(view);this.mImage = GameGlobal.get_pic();preview_img = (ImageView)view.findViewById(R.id.pref_current_img);preview_img.setImageResource(this.mImage);}    @Override    protected void onClick() {        super.onClick();        Bundle bundle = new Bundle();        bundle.putInt(GameGlobal.PREF_KEY_IMAGE, this.mImage);        Intent intent = new Intent(parent, ImageSelector.class);        intent.putExtras(bundle);        parent.startActivityForResult(intent, MagicSetting.REQUEST_CODE_GAME_IMAGE);            }}
对应的 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 就是在选项右边显示的图片。

更多相关文章

  1. 浅谈android的selector背景选择器
  2. Android(安卓)UI设计——ImageView和ImageButton控件
  3. Android中的Selector
  4. Android静态图片人脸识别的完整demo(附完整源码)
  5. Android菜鸟日记12 Gallery
  6. android 获取http网络图片保存png
  7. ImageView的属性大全
  8. 编写自定义的 Android(安卓)Preference 组件
  9. android 按钮设计中state_selected属性

随机推荐

  1. Android中级教程之----Log图文详解(Log.v
  2. Android 在安装完成界面,点击打开应用程序
  3. Android代码混淆配置(Proguard文件解析)
  4. 简述Android触摸屏手势识别
  5. android软键盘的显示后隐藏
  6. android悬浮按钮(Floating action button)
  7. Android输入法之——如何在代码中强制切
  8. Flutter学习笔记第一天:flutter的安装和初
  9. 如何在Android(安卓)Studio中添加Recycle
  10. 'windowBackground' 无法找到问题