使用google默认的PreferenceActiivty实现了该功能,但都是默认的背景和字体。怎么才改变它们的样式呢?PreferenceActivity继承ListActivity,所以本质上它是列表,所以可以通过得到它的ListView来设置背景或其它样式,如:
getListView().setBackgroundColor(Color.BLUE);
复制代码
背景颜色是变了,当时当你滚动这个列表项时候,是不是会发现后面好像还有黑色背景。那怎么去掉这个黑色背景呢?只需要改变它的缓存色为透明即可,如:
getListView().setCacheColorHint(Color.TRANSPARENT);
复制代码
这样就解决了滚动黑屏的问题了。其实还有一个地方的样式不知道怎么修改。就是PreferenceCategory(偏好分类)的样式怎么修改呢?我参考了其它人的一些观点,发现都没有生效。最后看了PreferenceCategory的源码,发现了该类的注释:Used to group {@link Preference} objects * and provide a disabled title above the group.这说明这上面标题是disabled的。那怎么才可以修改呢?最后经过测试,发现自定义一个PrefereceCategory可以达到自己的要求,java代码如下:
package com.rlht.enforce.widget;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.preference.PreferenceCategory;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;
/**
* 自定义设置Preference的category。google默认的Category无法提供修改样式的接口
* @author Zhuhanshan
*
*/
public class MyPreferenceCategory extends PreferenceCategory{
public MyPreferenceCategory(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onBindView(View view) {
super.onBindView(view);
view.setBackgroundColor(Color.GRAY);
if(view instanceof TextView){
TextView tv = (TextView) view;
tv.setTextSize(18);
}
}
}
复制代码
然后在peference.xml中使用这个自定义组件即可,xml如下:
<com.rlht.enforce.widget.MyPreferenceCategory android:title="@string/base_setting">
<EditTextPreference android:key="username"
android:defaultValue="@null"
android:title="@string/username"/>
<EditTextPreference android:key="password"
android:defaultValue="@null"
android:title="@string/password" />
</com.rlht.enforce.widget.MyPreferenceCategory>
复制代码
分享到:
相关推荐
android Preference自定义样式
Android Preference Framework源码~~~~~~~~~~~~~~~~~
上篇博文代码实现了Android自带的preference组件,本文将通过实例讲解自定义preference组件。 主要通过以下几步来实现: 1.定义需要的layout布局res->layout->xml文件; 2.通过继承Preference类,来实现自定义...
Preference,比如系统设置、短信都可以使用,简单而又好使用,而且Google原生Android代码中大量的使用了Preference组件。简单说, Preference组件其实就是Android常见UI组件与SharePreferences的组合封装实现。
Android中preference的使用,使用详解.
Android中Preference的使用以及监听事件分析
Android提供preference这个键值对的方式来处理这种情况,自动保存这些数据,并立时生效,同时Android提供一种类似的layout的方式来进行Preference的布局。 Preference组件有ListPreference,EditTextPreference,...
此DEMO内含基本的android preference framework的简单介绍,包括CheckboxPreference, RingtonePreference, EditTextPreference以及ListPreference。主要探究了一下android 怎么通过使用preference从而达到对用户定制...
android程序的一个demo,演示Preference保存设置的功能
安卓Android源码——Preference_Demo.rar
NULL 博文链接:https://sunney2012.iteye.com/blog/1158214
android-support-v7-preference.jar
Android preference activity sample.
这是Pro Android学习系列中preference部分的例子源代码。
自定义preference的checkboxpreference样式
android各种Preference的使用
Based on support-preference from Android Support Library, adding a lot of exciting features. Sample How to use add dependencies // replace with version above implementation 'moe.shizuku.preference...
Material Preference Library uses com.android.support:preference-v7:x.x.x support library widgets. Also it includes a color chooser dialog widget, that can be used to select accent color . Header is ...
Android程序 Preference应用 注册案例
主要介绍了Android设置PreferenceCategory背景颜色的方法,涉及Android设置背景色的技巧,需要的朋友可以参考下