- 浏览: 32703 次
- 性别:
- 来自: 深圳
最新评论
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中
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中
发表评论
-
for,foreach,iterator的用法和区别
2015-01-05 23:06 1030相同点: 三个都可以用来遍历数组和集合 不同点:1.形式差 ... -
Android网络开发之实时获取最新数据
2014-12-14 16:13 841在实际开发中更多的是需要我们实时获取最新数据,比如道路流量、实 ... -
底部菜单栏之Fragment的详细介绍和使用方法
2014-11-21 00:18 880底部菜单栏之Fragment的详细介绍和使用方法 : http ... -
Android--添加子视图(addView和setView)
2014-10-24 01:57 1978我们在添加视图文件的时候有两种方式,一种是通过在xml文件定义 ... -
Android 启动Launcher的说明
2014-10-24 01:26 774LauncherApplication->onCreat ... -
android动态布局方法总结
2014-09-19 00:21 704//绝对布局 AbsoluteLayout abslayout ... -
android回调函数总结
2014-08-28 00:12 789android回调函数总结 回调函数就是那些自己写的,但是不 ... -
修改设置Android Preference相关样式
2014-08-14 00:51 4534使用google默认的PreferenceActiivty实现 ... -
安卓cpu超频降频
2014-08-05 00:49 9201.Android获取系统cpu信息,内存,版本,电量等信息 ... -
ndroid进程间通信:使用AIDL
2014-07-24 00:17 641要进程间通信、需要AIDL(以及Binder),那么可以默认你 ... -
Android 系统及framework 概述
2014-06-27 22:41 720初次接触android的朋友,首先需要对android系 ... -
ubuntu下创建eclipse快捷方式
2014-05-15 23:21 910ubuntu下创建eclipse快捷方式 1、 sudo ... -
android之 IntentFilter 应用
2014-05-03 00:03 655当Intent在组件间传递时 ... -
Android开发的广播机制讲解
2014-05-03 00:01 835概述 在Android中,有一些操作完成以后,会发送广播,比 ...
相关推荐
文件是chm格式的,类似于Java的帮助文档,是我从开始接触Android,一点一滴总结Android的技术知识,总的来说是适合初学者来学习的,而且这个chm文档搜索功能相当强大,搜索某个关键词就可以搜索到对应的知识
android基本知识点总结,比较详细,经常复习
Android开发知识点总结[参考].pdf
android重要知识点总结 面试必备 android重要知识点总结 面试必备
android基础知识点总结(111页)
安卓知识点详细介绍,便于复习总结等等。安卓知识点详细介绍,便于复习总结等等。安卓知识点详细介绍,便于复习总结等等。安卓知识点详细介绍,便于复习总结等等。安卓知识点详细介绍,便于复习总结等等。安卓知识点...
Android开发各知识点总结,关于android开发的安全性问题的
精致排版,没有广告 大纲: 一、Java知识点汇总 二、Android知识点汇总 三、Android扩展知识点 四、Android开眼库源码分析 五、设计模式汇总 六、Gradle知识点汇总 七、常见面试算法题汇总
Android 常见知识点总结以及基础知识问题
android基本知识点总结.doc
Android知识系统总结需要就下吧,我也在学习中!
Android核心知识点笔记
收集的Android核心知识点笔记,JAVA知识点汇总、Android知识点汇总、Android拓展知识点、Android开源库源码分析、设计模式汇总、Gradle知识点汇总、常见面试算法题汇总。Android
android相关知识总结android相关知识总结android相关知识总结
android基本知识点总结.pdf
Android入门到精通知识总结,适合与Android知识的中初级学习者,个人Android开发实例及经验总结
学习android需要的Java知识点列表.pdf 一个都不能少
Android开发知识点总结.pdf