Android PrefeneceActivity小结

1.PrefeneceActivity都提供了哪几种元素可供使用

      image

各个对象的继承关系(看sdk api)

Preference ---> CheckBoxPreference
DialogPreference
-------> EditTextPreference, ListPreference,
PreferenceGroup
-------> PreferenceCategory, PreferenceScreen
RingtonePreference

1)PreferenceScreen:PreferenceActivity的根元素,必须为它。

2)PreferenceCategory:用于分组。效果如下:

          image

3)Preference:只进行文本显示,需要与其他进行组合使用。

           image

系统提供几种标准的preference:(扩展自preference)

CheckBoxPreference:CheckBox选择项,对应的值的ture或flase。如图:

            image

EditTextPreference:输入编辑框,值为String类型,会弹出对话框供输入。

image 

ListPreference: 列表选择,弹出对话框供选择。

image 

RingtonePreference:系统玲声选择。

         image

如果自己对这些系统样式还不满足,自己可以对preference进行扩展.如下图的扩展效果:

    

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

该效果代码如下:

ImageOptionPreference.java
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 配置信息如下:(在res/xml/preference.xml)

preference.xml
< 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 的信息如下:(在res/layout/preference_widget_image.xml)

preference_widget_image.xml
<? 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 "
/>

该例子代码转载自:http://www.oschina.net/question/12_2175


2.自定义Preference样式,PreferenceActivity中添加普通view组件

在一个集成PreferenceActivity的类中,可以通过

addPreferencesFromResource(R.xml.preference);加载preference控件

还可以通过setContentView(R.layout.preference_layout)来自定义preference内容。

案例:现在想在PreferenceActivity添加一个Button

1)、新建一个Layout,文件名为set_preference_main.xml,文件内容如下:

set_preference_main.xml
<? xml version = " 1.0 " encoding = " utf-8 " ?>
< LinearLayout
xmlns:android
= " http://schemas.android.com/apk/res/android "
android:layout_width
= " fill_parent "
android:layout_height
= " fill_parent "
android:orientation
= " vertical " >
< Button
android:layout_width
= " fill_parent "
android:layout_height
= " wrap_content "
android:text
= " Button " ></ Button >

< ListView android:id = " @android:id/list "
android:layout_width
= " fill_parent "
android:layout_height
= " wrap_content " ></ ListView >
</ LinearLayout >
其中Button为自己需要添加的view, ListView会被R.xml.preference的preferences替换。
注意其中ListView的android:id="@android:id/list"必须,且不可改变。
(特别注意,一定要加这个listview控件,不然运行后会出错,而且可以通过这个listview控件来定义样式,如透明之类的!!)

其实可以理解为:preference就是一个listview。

2)、在Activity的onCreate中添加:

public void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);

addPreferencesFromResource(R.xml.preference);
setContentView(R.layout.set_preference_main);
}

注意其中setContentView(R.layout.set_preference_main);表示加载set_preference_main.xml内容到content中

转载于:https://www.cnblogs.com/playing/archive/2011/07/04/2097283.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值