Android Preference修改点击条目的背景色
来源:互联网 发布:淘宝首页链接一键生成 编辑:程序博客网 时间:2024/05/22 06:37
很多朋友在用Preference时,都觉得这个控件好用,但是很多的东西不能自己定制,比如字体大小,背景色,点击背景色等等,大家都知道这个控件是放在framework中的,总不能为了一个小需求就去改动framework的代码吧,本文就以修改点击背景色为例,提供一种在不改动framework的前提下定制自己风格的Preference的思路,首先,我们先来看看Preference的源码,
在路径platform_frameworks_base/core/java/android/preference/Preference.java
下,
/** * Creates the View to be shown for this Preference in the * {@link PreferenceActivity}. The default behavior is to inflate the main * layout of this Preference (see {@link #setLayoutResource(int)}. If * changing this behavior, please specify a {@link ViewGroup} with ID * {@link android.R.id#widget_frame}. * <p> * Make sure to call through to the superclass's implementation. * * @param parent The parent that this View will eventually be attached to. * @return The View that displays this Preference. * @see #onBindView(View) */ @CallSuper protected View onCreateView(ViewGroup parent) { final LayoutInflater layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); final View layout = layoutInflater.inflate(mLayoutResId, parent, false); final ViewGroup widgetFrame = (ViewGroup) layout .findViewById(com.android.internal.R.id.widget_frame); if (widgetFrame != null) { if (mWidgetLayoutResId != 0) { layoutInflater.inflate(mWidgetLayoutResId, widgetFrame); } else { widgetFrame.setVisibility(View.GONE); } } return layout; }
Preference的onCreateView方法里加载了一个mLayoutResId的布局,而这个布局变量定义在
private int mLayoutResId = com.android.internal.R.layout.preference;
如果没有写死,而是用一个变量代表布局,那就说明必定对外提供了修改这个成员变量的方法,
public void setLayoutResource(@LayoutRes int layoutResId) { if (layoutResId != mLayoutResId) { // Layout changed mRecycleEnabled = false; } mLayoutResId = layoutResId; }
通过这个方法可以设置自己的Preference布局,那么现在就可以总结一下步骤了,
第一步,重写一个类继承Preference,重写构造方法等一些方法。
第二步,仿照Preference的布局重写一套布局,在根布局里修改background属性为自己的selector。
第三步,在重写的Preference的构造函数里调用setLayoutResource设置自己的自定义布局。
第四步,将原本的Preference替换为自己的布局。
这样就可以定制自己的Preference了,什么字体什么颜色都可以按照自己的意愿在自己的布局修改。
阅读全文
0 0
- Android Preference修改点击条目的背景色
- Android ListView点击行高亮(修改背景色)
- android 修改背景色
- Android actionBar 修改背景色
- android 修改主题背景色
- Android修改状态栏背景色
- Android actionBar 修改背景色
- android修改ActionBar背景色
- Android中取消GridView & ListView默认的点击背景色
- 如何修改android studio背景色(包括project view的背景色)保护眼睛人人有责
- 修改command的背景色
- RichEdit背景色的修改
- 修改eclipse的背景色
- 修改chrome的背景色
- 修改eclipse的背景色
- 修改eclipse的背景色
- 修改eclipse的背景色
- 修改eclipse的背景色
- 理解Python的With语句
- 简单数论总结
- Java:单例模式的七种写法
- 通达OA应对后门检测的临时方法
- SVN错误:Attempted to lock an already-locked dir
- Android Preference修改点击条目的背景色
- idea中使用mybatis逆向工程详解
- 关于activity保存页面状态的两个方法
- 01.node简单form提交
- jQuery Mobile中列表listview(ol、ul)的data-*选项
- 快速学会使用MyBatis
- 滴滴2017秋招编程题
- 黑马day14 过滤器之自动登录小案例
- java中throws关键字