編寫自定義的 Android Preference 組件
Android SDK 提供好幾個 Preference 組件,例如 CheckBoxPreference、EditTextPreference、DialogPreference、ListPreference 等,這些組件是跟 Android 提供的 Preference 存儲機製綁定的,你可以通過這些組件來修改應用的一些配置,如下圖所示,這是 Android 自帶的係統設置界麵:
但這些組件畢竟還不能滿足100%的要求,假設我們需要為應用程序提供一個選擇不同圖片做為應用背景圖的設置,我們需要一個很直觀的就可以看到當前所選擇的圖片,然後點擊後可以瀏覽其他圖片並選擇。那麼這些 Preference 就無法滿足這個需求,因此我們需要對 Preference 進行擴展,下圖是擴展後的效果:
請看中間選項的效果,在右邊顯示當前選擇的圖片。
代碼如下:
- 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;
- }
- @Override
- public 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 ;
- }
- @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);
- }
- @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);
- }
- }
- 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;
}
@Override
public 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;
} - @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);
} - @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);
} - }
<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:andro
android:
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 就是在選項右邊顯示的圖片。
最後更新:2017-04-02 06:51:49