- 浏览: 128959 次
- 性别:
- 来自: 北京
最新评论
-
qiuhuahui1:
真的有用,帮我节省了大把的时间。
android的ant编译打包 -
fxiaozj:
楼主,有没有demo?
Android ViewGroup实现页面滑动效果并实现不同的动画效果 -
sgjsdf5944:
你好,请问下楼主如果我想在安装完成页面上控制打开不可用该怎么实 ...
Android 监控程序安装和删除的实现 -
renfujiang:
养成好习惯,看过别人的文章 就得评论 增加点人气 好文章 我是 ...
android的ant编译打包 -
leishengwei:
你好,第一种方法在4.0时,输入法是弹不出去了,但是光标不能正 ...
Android如何关闭EditText中的软键盘
首先在res/xml文件夹下建立preferences.xml
然后在代码中加载preferences.xml
这样就创建了从xml加载preferences的默认的PreferenceActivity。
在加载了preferences.xml的PreferenceActivity中, a top-level preference是一个PreferenceScreen,可用getPreferenceScreen()获取。PreferenceScreen和PreferenceCategory继承自PreferenceGroup,它们可以包含一个或多个PreferenceScreen,PreferenceCategory或者是具体的preference(如EditTextPreference、CheckBoxPreference)。由于PreferenceScreen,PreferenceCategory,EditTextPreference等都是继承自Preference,因此可以通过setLayoutResource()方法设置自己的布局样式。下面将遍历所有Preference,并设置自己的样式,代码如下:
preference_screen.xml
preference_category.xml
preference.xml
下面介绍加顶部布局,其实也是添加加一个preference,通过preferenceScreen的addPreference添加。首先自定义一个PreferenceHead,布局中有一个返回按钮。
然后在代码中实现
这样就完成了一个具有返回按钮的顶部布局的 PreferenceActivity,效果图如下
<?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" > <PreferenceCategory android:title="inline_preferences" > <CheckBoxPreference android:key="checkbox_preference" android:summary="summary_toggle_preference" android:title="title_toggle_preference" /> </PreferenceCategory> <PreferenceCategory android:title="dialog_based_preferences" > <EditTextPreference android:dialogTitle="dialog_title_edittext_preference" android:key="edittext_preference" android:summary="summary_edittext_preference" android:title="title_edittext_preference" /> <ListPreference android:dialogTitle="dialog_title_list_preference" android:entries="@array/entries_list_preference" android:entryValues="@array/entryvalues_list_preference" android:key="list_preference" android:summary="summary_list_preference" android:title="title_list_preference" /> </PreferenceCategory> <PreferenceCategory android:title="launch_preferences" > <PreferenceScreen android:key="screen_preference" android:summary="summary_screen_preference" android:title="title_screen_preference" > <CheckBoxPreference android:key="next_screen_checkbox_preference" android:summary="summary_next_screen_toggle_preference" android:title="title_next_screen_toggle_preference" /> </PreferenceScreen> <PreferenceScreen android:summary="summary_intent_preference" android:title="title_intent_preference" > <intent android:action="android.intent.action.VIEW" android:data="http://www.android.com" /> </PreferenceScreen> </PreferenceCategory> <PreferenceCategory android:title="preference_attributes" > <CheckBoxPreference android:key="parent_checkbox_preference" android:summary="summary_parent_preference" android:title="title_parent_preference" /> <CheckBoxPreference android:dependency="parent_checkbox_preference" android:key="child_checkbox_preference" android:layout="?android:attr/preferenceLayoutChild" android:summary="summary_child_preference" android:title="title_child_preference" /> </PreferenceCategory> </PreferenceScreen>
然后在代码中加载preferences.xml
public class MyPreferenceActivity extends PreferenceActivity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.preferences); } }
这样就创建了从xml加载preferences的默认的PreferenceActivity。
在加载了preferences.xml的PreferenceActivity中, a top-level preference是一个PreferenceScreen,可用getPreferenceScreen()获取。PreferenceScreen和PreferenceCategory继承自PreferenceGroup,它们可以包含一个或多个PreferenceScreen,PreferenceCategory或者是具体的preference(如EditTextPreference、CheckBoxPreference)。由于PreferenceScreen,PreferenceCategory,EditTextPreference等都是继承自Preference,因此可以通过setLayoutResource()方法设置自己的布局样式。下面将遍历所有Preference,并设置自己的样式,代码如下:
private void setLayoutResource(Preference preference) { if (preference instanceof PreferenceScreen) { PreferenceScreen ps = (PreferenceScreen) preference; ps.setLayoutResource(R.layout.preference_screen); int cnt = ps.getPreferenceCount(); for (int i = 0; i < cnt; ++i) { Preference p = ps.getPreference(i); setLayoutResource(p); } } else if (preference instanceof PreferenceCategory) { PreferenceCategory pc = (PreferenceCategory) preference; pc.setLayoutResource(R.layout.preference_category); int cnt = pc.getPreferenceCount(); for (int i = 0; i < cnt; ++i) { Preference p = pc.getPreference(i); setLayoutResource(p); } } else { preference.setLayoutResource(R.layout.preference); } }
preference_screen.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="wrap_content" android:gravity="center_vertical" android:minHeight="?android:attr/listPreferredItemHeight" android:paddingRight="?android:attr/scrollbarSize" > <ImageView android:id="@+android:id/icon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:src="@drawable/ic_launcher"/> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="6dip" android:layout_marginLeft="15dip" android:layout_marginRight="6dip" android:layout_marginTop="6dip" android:layout_weight="1" > <TextView android:id="@+android:id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ellipsize="marquee" android:fadingEdge="horizontal" android:singleLine="true" android:textAppearance="@android:style/TextAppearance.Large" android:textColor="#FFFF1234" /> <TextView android:id="@+android:id/summary" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@android:id/title" android:layout_below="@android:id/title" android:maxLines="4" android:textAppearance="@android:style/TextAppearance.Small" android:textColor="#FF888888" /> </RelativeLayout> <LinearLayout android:id="@+android:id/widget_frame" android:layout_width="wrap_content" android:layout_height="match_parent" android:gravity="center_vertical" android:orientation="vertical" /> </LinearLayout>
preference_category.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="wrap_content" android:background="#FF123456" android:gravity="center_vertical" android:minHeight="?android:attr/listPreferredItemHeight" android:paddingRight="?android:attr/scrollbarSize" > <ImageView android:id="@+android:id/icon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:src="@drawable/ic_launcher" /> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="6dip" android:layout_marginLeft="15dip" android:layout_marginRight="6dip" android:layout_marginTop="6dip" android:layout_weight="1" > <TextView android:id="@+android:id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ellipsize="marquee" android:fadingEdge="horizontal" android:singleLine="true" android:textAppearance="@android:style/TextAppearance.Large" android:textColor="#FFFF0000" /> <TextView android:id="@+android:id/summary" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@android:id/title" android:layout_below="@android:id/title" android:maxLines="4" android:textAppearance="@android:style/TextAppearance.Small" android:textColor="#FF00FF00" /> </RelativeLayout> <LinearLayout android:id="@+android:id/widget_frame" android:layout_width="wrap_content" android:layout_height="match_parent" android:gravity="center_vertical" android:orientation="vertical" /> </LinearLayout>
preference.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="wrap_content" android:gravity="center_vertical" android:minHeight="?android:attr/listPreferredItemHeight" android:paddingRight="?android:attr/scrollbarSize" > <ImageView android:id="@+android:id/icon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" /> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="6dip" android:layout_marginLeft="15dip" android:layout_marginRight="6dip" android:layout_marginTop="6dip" android:layout_weight="1" > <TextView android:id="@+android:id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ellipsize="marquee" android:fadingEdge="horizontal" android:singleLine="true" android:textAppearance="@android:style/TextAppearance.Medium" android:textColor="#FF00FFFF" /> <TextView android:id="@+android:id/summary" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@android:id/title" android:layout_below="@android:id/title" android:maxLines="4" android:textAppearance="@android:style/TextAppearance.Small" android:textColor="#FFFFFF00" /> </RelativeLayout> <LinearLayout android:id="@+android:id/widget_frame" android:layout_width="wrap_content" android:layout_height="match_parent" android:gravity="center_vertical" android:orientation="vertical" /> </LinearLayout>
下面介绍加顶部布局,其实也是添加加一个preference,通过preferenceScreen的addPreference添加。首先自定义一个PreferenceHead,布局中有一个返回按钮。
package com.preference.main; import android.content.Context; import android.preference.Preference; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class PreferenceHead extends Preference { private OnClickListener onBackButtonClickListener; public PreferenceHead(Context context) { super(context); setLayoutResource(R.layout.preference_head); } @Override protected void onBindView(View view) { super.onBindView(view); Button btBack = (Button) view.findViewById(R.id.back); btBack.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (onBackButtonClickListener != null) { onBackButtonClickListener.onClick(v); } } }); } public void setOnBackButtonClickListener(OnClickListener onClickListener) { this.onBackButtonClickListener = onClickListener; } }
<?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="60.0dip" android:background="#8000FF00" android:gravity="center_vertical" > <Button android:id="@+id/back" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10.0dip" android:text="返回" /> </LinearLayout>
然后在代码中实现
PreferenceHead ph = new PreferenceHead(this); ph.setOnBackButtonClickListener(new OnClickListener() { @Override public void onClick(View v) { finish(); } }); ph.setOrder(0); preferenceScreen.addPreference(ph);
这样就完成了一个具有返回按钮的顶部布局的 PreferenceActivity,效果图如下
发表评论
-
ListView与Button、imageButton 的共存问题解决
2013-02-20 11:39 1303ListView与Button、imageButton 的共存 ... -
android实现popupwindow的动画效果
2013-02-01 18:56 1990问题:在打开或者关闭popupwindow的时候怎么样显示动画 ... -
Android开发ViewPager中ListView失效问题解决方法
2013-01-28 22:12 2748最近开发一个Android小应用。就是利用ViewPager实 ... -
Android ViewGroup实现页面滑动效果并实现不同的动画效果
2012-08-28 22:24 3116这应该是自己第一次开始写博客,今天起想要记录下自己工作上学到的 ... -
关于WebView的loadData方法以及乱码问题
2012-08-09 14:37 952WebView是Android应用开发 ... -
Android对图片的压缩读取和保存
2012-08-08 10:27 1370在开发图片浏览器等软件是,很多时候要显示图片的缩略图,而一般情 ... -
ubuntu下载android源代码
2012-07-22 00:50 1892用虚拟机来创建一个Ubun ... -
android面试(2)
2012-07-20 16:17 1062附带答案,共100分 一、选择题(30题,每题1.5分,共4 ... -
android拦截短信并屏蔽系统的Notification
2012-05-21 09:25 1781Android短信拦截,总的来说有两种方式: (一)、在代码 ... -
Android设置应用程序默认语言
2012-05-19 21:03 1517Android应用程序的国际化还是做得不错的,通过设置资源文件 ... -
(android 实战总结)android对html支持接口总结
2012-04-18 09:50 2368Android支持html 的两个接口 1 Spanned ... -
Android的TextView使用Html来处理图片显示、字体样式、超链接等
2012-04-16 11:09 1545转eoe:http://www.eoeandroid.com/ ... -
android的ant编译打包
2012-04-11 14:52 3894Android本身是支持ant打包项目的,并且SDK中自带一个 ... -
使用InputStreamEntity 边读取边上传文件
2012-04-09 17:32 7402HttpClient httpclient = new Def ... -
android xliff字符串操作
2012-04-09 14:59 1407参考:http://blog.csdn.net/freshma ... -
Android:只读EditText内容可滚动(禁止输入法)的实现
2012-04-06 12:00 1385实验设备为HTC hero (SDK 2.1-update1) ... -
Eclipse报内存溢出
2012-04-04 10:50 1090(1)在配置tomcat的JDK里面设置。Window--&g ... -
Android解压缩zip的实现
2012-03-20 11:21 2817android 解压缩zip包,需要在menifest.xml ... -
Android alertdialog的按钮点击后不消失
2012-03-15 20:35 1857使用反射: 在你的setPositiveButton中添加 ... -
Android 读取<meta-data>元素的数据
2012-03-05 13:47 1936在AndroidManifest.xml中,<meta- ...
相关推荐
自定义PreferenceActivity的样式,相关博客:http://www.cnblogs.com/tianzhijiexian/p/3892506.html
自定义PreferenceActivity,仿照360手机卫士的设置界面做的,详情前看代码。
很想做个天气预警的功能, 想用preferences来做界面。 看了很多preferences感觉定制性太差 所以自己做了一个。 应该是不错的demo 定制preferences在preferencesActivity中的布局 和自定义了对话框的布局
Android 自定义PreferenceActivity
导语:PreferenceActivity是一个方便设置管理的界面,但是对于界面显示来说比较单调,所以自定义布局就很有必要了。本文举例说明在Preference中自定义layout的方法。笔者是为了在设置中插入@有米v4广告条才研究了一...
PreferenceActivity用法简介 Android X
该资源包含了一个android自定义的preferenceActivity简单易懂,喜欢的下载!
这个是我将设置中的PreferenceActivity修改成Activity的过程,本dome是以 WifidisplayActivity为例子。
使用 PreferenceActivity设置个性信息, 参考:android 设置类PreferenceActivity http://blog.csdn.net/xiaobijia/article/details/41479235
最近研究了Android Setting中源码部分,发现大多视图显示部分大多采用的是Preference,所以我修改了系统设置中的一个界面希望能帮助到大家
NULL 博文链接:https://f059074251.iteye.com/blog/1672151
NULL 博文链接:https://2528.iteye.com/blog/1049177
android中PreferenceActivity详解
雨松MOMO带你做软件 Android软件开发之PreferenceActivity中组件的使用 ,欢迎大家下载阅读 哇咔咔~~
仿IOS的PreferenceActivity界面,素材来自新浪微博,相关博文:http://www.cnblogs.com/tianzhijiexian/p/3893504.html
android PreferenceActivity中的组件源码~
PreferenceActivity和ExpandableListActivity的使用,详细了解请移步:http://blog.csdn.net/zxc514257857/article/details/77773001