自定义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);
}

}

对应的 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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值