阅读更多 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)
复制代码
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"?>


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"?>
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">


android:layout_width="fill_parent"
android:layout_height="wrap_content">


复制代码
其中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中

更多相关文章

  1. Android下MP3播放器的实现源代码02
  2. Android下MP3播放器的实现源代码03
  3. Android Audio代码分析21 - 创建AudioEffect对象
  4. 点击listitem按下效果替换默认颜色
  5. 原创:Android 基础 控件 之 TextVIew(一)
  6. Android Drawable 在代码中实现android:tint效果
  7. Android电视关闭的动画效果
  8. Android Chromium的标题代码运行路径
  9. Android实用代码大全

随机推荐

  1. 小米官宣了个锤子?真相是爬虫的锅!
  2. 不知道JVM逃逸分析?看这篇文章就够了!
  3. 假如你是微博架构师,你会如何设计微博架构
  4. DoraOS连接Proxmox VE搭建简单桌面云
  5. 震惊,个性化的Diy智能制造工业4.0已提前到
  6. 当Solr遇上Elasticsearch,你该选择谁?
  7. 剧透,Nacos release 0.3.0 将在本周五开源
  8. Linux权限管理chmod 755(chmod xxx)教程
  9. 搞技术的程序员为什么都转型去做公众号了
  10. Hyperledger Fabric 超级账本视频教程免