首先,像这样的两种preference,一种是具有开关功能,一种是没有具有开关功能,第一是自定义下面的没具有开关功能的preference
首先是在布局文件里面定义好一个layout ,下面这个就不用说了
setting_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="44dp"
android:background="#FFFFFF"
android:orientation="horizontal" >
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:layout_gravity="center_vertical"
android:id="@+id/item_image"
android:paddingLeft="10dp"
/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="6"
android:id="@+id/item_title"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:textSize="16sp"
android:textColor="#3A3A3A"
android:paddingLeft="10dip"
/>
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:layout_gravity="center_vertical"
android:id="@+id/item_off_on"
android:paddingLeft="10dp"
android:paddingRight="10dp"
/>
</LinearLayout>
---------------------------------------------------------------------------------------------------------
public class IconPreference extends Preference implements OnClickListener,
OnTouchListener {
//
private Drawable mItemDrawable;
private Drawable mItemOffDrawable;
// 当前Layout
private View view;
private SettiingListener setListener;
public IconPreference(Context context) {
this(context, null);
// TODO Auto-generated constructor stub
}
public IconPreference(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.IconPreference);
//像下面这样的styleable是在attr里面自定义的属性。
int icon = a.getResourceId(R.styleable.IconPreference_head_image, 0);
mItemDrawable = context.getResources().getDrawable(icon);
//判断末尾是否有图片
int off_on = a.getResourceId(R.styleable.IconPreference_stern_image, 0);
if (off_on > 0) {
mItemOffDrawable = context.getResources().getDrawable(off_on);
}
a.recycle();
}
//回调接口
public interface SettiingListener {
public void setSettingListener(String key);
}
public void setListener(SettiingListener setListener) {
this.setListener = setListener;
}
@Override
protected View onCreateView(ViewGroup parent) {
// TODO Auto-generated method stub
//这个就是定义layout
return LayoutInflater.from(getContext()).inflate(R.layout.setting_view,
parent, false);
}
@Override
protected void onBindView(View view) {
// TODO Auto-generated method stub
this.view = view;
//这个是最前面的图片
ImageView icon = (ImageView) view.findViewById(R.id.item_image);
icon.setImageDrawable(mItemDrawable);
//这个是中间用到的文字
TextView title = (TextView) view.findViewById(R.id.item_title);
title.setOnTouchListener(this);
title.setText(getTitle());
//这个是末尾的图片,为什么要加个判断呢,是因为有的preference末尾并不一定有图片
if (mItemOffDrawable != null) {
ImageView off_on = (ImageView) view.findViewById(R.id.item_off_on);
off_on.setOnClickListener(this);
off_on.setImageDrawable(mItemOffDrawable);
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.item_off_on:
//这里的回调当然是为了activity根据所点击的key,处理对应的事件。
if (setListener != null) {
setListener.setSettingListener(getKey());
}
break;
}
}
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
//增加动画效果,当按下去,preference会缩小的效果
smallAnimation();
break;
case MotionEvent.ACTION_UP:
//这里自然是弹起恢复原状
bigAnimation();
break;
}
return true;
}
// 设置按下变小
private void smallAnimation() {
AnimationSet aa = new AnimationSet(true);
ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f, 0.96f, 1.0f,
0.8f, Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
aa.setDuration(500);
aa.setInterpolator(new LinearInterpolator());
aa.setFillEnabled(true);
aa.setFillAfter(true);
aa.addAnimation(scaleAnimation);
view.startAnimation(aa);
}
// 设置谈起恢复
private void bigAnimation() {
AnimationSet aa = new AnimationSet(true);
ScaleAnimation scaleAnimation = new ScaleAnimation(0.96f, 1.0f, 0.8f,
1.0f, Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
aa.setInterpolator(new LinearInterpolator());
aa.setFillAfter(true);
aa.setFillEnabled(true);
aa.addAnimation(scaleAnimation);
aa.setDuration(100);
view.startAnimation(aa);
}
}
-----------------------------------------------------------------------------------------------
下面是在attrs.xml里面定义的属性
<declare-styleable name="IconPreference">
//最前面个图片
<attr name="head_image" format="reference" />
//末尾图片
<attr name="stern_image" format="reference"/>
</declare-styleable>
-----------------------------------------------------------------------------------------------------------
下面这个是具有开关功能的preference,这个是继承上面的那个preference,点击图片的时候就多了一个开关处理。
public class IconCheckBoxPreference extends IconPreference implements
OnClickListener {
private boolean isOn = Boolean.valueOf(false);
private ImageView on_off;
public IconCheckBoxPreference(Context context) {
super(context);
initViewData(context, null);
// TODO Auto-generated constructor stub
}
public IconCheckBoxPreference(Context context, AttributeSet attrs) {
super(context, attrs);
initViewData(context, attrs);
// TODO Auto-generated constructor stub
}
private void initViewData(Context context, AttributeSet attrs) {
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.IconCheckBoxPreference);
//这个也是自定义属性,如果为true是打开,flase是关闭
isOn = a.getBoolean(R.styleable.IconCheckBoxPreference_isselect, isOn);
}
@Override
protected View onCreateView(ViewGroup parent) {
// TODO Auto-generated method stub
return LayoutInflater.from(getContext()).inflate(R.layout.setting_view,
parent, false);
}
@Override
protected void onBindView(View view) {
// TODO Auto-generated method stub
super.onBindView(view);
on_off = (ImageView) view.findViewById(R.id.item_off_on);
on_off.setOnClickListener(this);
setOnOff(isOn);
}
@Override
public void onClick(View v) {
isOn = !isOn;
setOnOff(isOn);
}
public void setOnOff(boolean isOn) {
//这里就是控制图片的开关
if (isOn) {
isOn = false;
on_off.setImageResource(R.drawable.set_on);
} else {
isOn = true;
on_off.setImageResource(R.drawable.set_off);
}
}
}
---------------------------------------------------------------------------------------------------------
这里也是在attrs.xml里面定义的属性
<declare-styleable name="IconCheckBoxPreference" parent="IconPreference">
//这个是定义开关的属性
<attr name="isselect" format="boolean"/>
</declare-styleable>
---------------------------------------------------------------------------------------------------------------
public class SettingsActivity extends PreferenceActivity implements
SettiingListener {
private IconCheckBoxPreference night_model;
private IconPreference cache_manager;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//这里是加载xml文件
addPreferencesFromResource(R.xml.settings);
//这个是设置preferenceactivity为白色背景
getListView().setBackgroundColor(Color.WHITE);
initPreference();
}
privagte void initPreference(){
night_model = (IconCheckBoxPreference) findPreference(getResources()
.getString(R.string.night_model));
night_model.setListener(this);
cache_manager = (IconPreference) findPreference(getResources()
.getString(R.string.cache_manager));
}
@Override
public void setSettingListener(String key) {
// TODO Auto-generated method stub
//这就是你点击的图片所对应的key值,根据需求 做你想做的事。
}
}
----------------------------------------------------------------------------------------------------------
然后就是在settings.xml用你刚才自定义的两个preference
首先就是定义你自己的属性的命名空间
xmlns:whty="http://schemas.android.com/apk/res/you.package"
<you.package.IconCheckBoxPreference
android:key="@string/night_model"
android:title="@string/night_model"
whty:head_image="@drawable/set_icon_night_model"
whty:isselect="false"
/>
<you.package.IconPreference
android:key="@string/cache_manager"
android:title="@string/cache_manager"
whty:head_image="@drawable/set_icon_cache_mger"
whty:stern_image="@drawable/arrow"
/>
就这样,所有涉及到的东西都在这里,