0
点赞
收藏
分享

微信扫一扫

编写自定义的 Android Preference 组件


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

 

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

请看中间选项的效果,在右边显示当前选择的图片。

代码如下:

 


​​源码​​​ ​​​打印​​​ ​​​?​​

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


1. 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;2. /**
* 图片选项,用于设置图片和边框
* @author Winter Lau
*/
public class ImageOptionPreference extends Preference {3. 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);
}4. public ImageOptionPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}5. public ImageOptionPreference(Context context) {
super(context);
}

void setActivity(PreferenceActivity parent) {
this.parent = parent;
}

@Override
public boolean isPersistent() {
return false;
}6. /**
* 修改图片
* @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;
}7. @Override
protected 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);
}8. @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);
}9. }

对应的 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 条评论