LayoutInflater.Factory 妙用
LayoutInflater.Factory 可以调试 布局,你把下面的代码放到
onCreate里面,然后再里面的下面的onCreateView里面加上断点,然后你就可以知道所有的view构造,所有的view的名字,不管是自定义还是系统的。
感觉挺不错的。
protected void setMenuBackGround() {
LayoutInflater layoutInflater = getLayoutInflater();
final LayoutInflater.Factory existingFactory = layoutInflater.getFactory();
// use introspection to allow a new Factory to be set
try {
Field field = LayoutInflater.class.getDeclaredField("mFactorySet");
field.setAccessible(true);
field.setBoolean(layoutInflater, false);
getLayoutInflater().setFactory(new LayoutInflater.Factory() {
@Override
public View onCreateView(String name, final Context context, AttributeSet attrs) {
if (name
.equalsIgnoreCase("android.support.v7.internal.view.menu.ListMenuItemView")) {
try {
LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View view = inflater.createView(name,
null, attrs);
new Handler().post(new Runnable() {
public void run() {
ViewParent parent = view.getParent();
((ViewGroup) parent).setBackgroundResource(R.color.test);
// view.setBackgroundResource(R.color.test);
}
});
return view;
} catch (InflateException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
// do whatever you want with the null or non-null view
// such as expanding 'IconMenuItemView' and changing its style
// or anything else...
// and return the view
return null;
}
});
} catch (NoSuchFieldException e) {
// ...
} catch (IllegalArgumentException e) {
// ...
} catch (IllegalAccessException e) {
// ...
}
}
然后这个类也很强大,可以做很多事情,向下兼容。等等,替换东西等等。
LayoutInflater.Factory 妙用的更多相关文章
- LayoutInflater.Factory的夜间模式
自定义实现个Factory,可以用来解析自定义的属性.public interface Factory { /** * Hook you can supply that is called when ...
- Android中将xml布局文件转化为View树的过程分析(下)-- LayoutInflater源码分析
在Android开发中为了inflate一个布局文件,大体有2种方式,如下所示: // 1. get a instance of LayoutInflater, then do whatever yo ...
- View.inflate和LayoutInflater的inflate方法区别
平时ListView加载item中,adapter的getView方法中,我们经常用到: LayoutInflater.from(mContext).inflate(R.layout.it ,pare ...
- Android LayoutInflater&LayoutInflaterCompat源码解析
本文分析版本: Android API 23,v4基于 23.2.1 1 简介 实例化布局的XML文件成相应的View对象.它不能被直接使用,应该使用getLayoutInflater()或getSy ...
- View inflate方法和LayoutInflater inflate方法的区别详解
原创文章,转载请注明出处:http://www.cnblogs.com/baipengzhan/p/6257510.html 我们在Android开发中,对于将布局填充成View对象,最常用的两种办法 ...
- LayoutInflater 原理分析 示例
LayoutInflater简介 LayoutInflater 顾名思义就是布局填充器,做过Android界面编程,相信对这个类都比较熟悉,可能有人说,我们在activity中使用set ...
- 获取LayoutInflater对象的方法和inflate方法的一些参数问题
一.获取LayoutInflater的三种方法 1. LayoutInflater layoutInflater = (LayoutInflater) MainActivity.this.getSys ...
- [Android]异步 layout inflation(翻译)
以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/5829809.html 异步 layout inflation ...
- Android Weekly Notes Issue #220
Android Weekly Issue #220 August 28th, 2016 Android Weekly Issue #220 ARTICLES & TUTORIALS Manag ...
随机推荐
- Struts_ActionWildcard_通配符配置
使用通配符,将配置量降到最低 不过,一定要遵守“约定由于配置”的原则 struts2.xml <?xml version="1.0" encoding="UTF-8 ...
- Moodle-3.1.2 (Ubuntu 16.04 )
平台: Ubuntu 类型: 虚拟机镜像 软件包: moodle-3.1.2 commercial education moodle open-source 服务优惠价: 按服务商许可协议 云服务器费 ...
- 服务器raid状态监控
参考 http://hwraid.le-vert.net/wiki 查看raid芯片 lspci -v 02:00.0 SCSI storage controller: LSI Logic / Sym ...
- PHP判断变量是否小数并对小数进行处理
/*判断是否为小数demo*/$a = 1.2; if(is_int($a)){ echo "$a 是整数!"; }else{ echo "$a 不是整数!"; ...
- vs下如何调试Dll
1.首先需要一个exe加载你的dll 2.dll项目的属性设置 3.将dll设为启动项 4.在dll中设置断点 F5就可以调试了
- April 6 2017 Week 14 Thursday
If you smile when no one else is around, you really mean it. 独处时的微笑,才是发自内心的. Recently I found I seld ...
- 2018.7.18 div,section,article的区别和使用
section ·<section> 标签定义文档中的节(section.区段).比如章节.页眉.页脚或文档中的其他部分. ·section用作一段有专题性的内容,一般在它里面会带有标题. ...
- npy数据的保存与读取
保存 利用这种方法,保存文件的后缀名字一定会被置为.npy x = numpy.save("data_x.npy",x) 读取 data = numpy.load("da ...
- myeclipse从SVN上拉项目,各种报错,jar包没有引入
问:项目中myeclipse从SVN上拉项目,各种报错,jar包没有引入 答: 从SVN拉项目步骤一定不能出错,一有点差异就会出非常多的事情 1-右键项目checkout的时候 第一页选默认值就行 点 ...
- ethereum(以太坊)(二)--合约中属性和行为的访问权限
pragma solidity ^0.4.0; contract Test{ /* 属性的访问权限 priveta public internal defualt internal interlnal ...