`
weicaijin8
  • 浏览: 32703 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

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进行扩展.如下图的扩展效果:

   

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

该效果代码如下:
复制代码
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
*/
publicclass ImageOptionPreference extends Preference {

private PreferenceActivity parent;
privateint 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
publicboolean isPersistent() {
returnfalse;
}

/**
* 修改图片
* @param newImage
* @return
*/
boolean ChangeGamePic(int newImage ){
if(this.mImage == newImage)
returnfalse;
GameGlobal.save_pic(newImage);
this.mImage = newImage;
preview_img.setImageResource(newImage);
returntrue;
}

@Override
protectedvoid 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
protectedvoid 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)
复制代码
<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)
复制代码
<?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,文件内容如下:
复制代码
<?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中添加:
publicvoid 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中
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics