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 妙用的更多相关文章

  1. LayoutInflater.Factory的夜间模式

    自定义实现个Factory,可以用来解析自定义的属性.public interface Factory { /** * Hook you can supply that is called when ...

  2. Android中将xml布局文件转化为View树的过程分析(下)-- LayoutInflater源码分析

    在Android开发中为了inflate一个布局文件,大体有2种方式,如下所示: // 1. get a instance of LayoutInflater, then do whatever yo ...

  3. View.inflate和LayoutInflater的inflate方法区别

    平时ListView加载item中,adapter的getView方法中,我们经常用到: LayoutInflater.from(mContext).inflate(R.layout.it ,pare ...

  4. Android LayoutInflater&LayoutInflaterCompat源码解析

    本文分析版本: Android API 23,v4基于 23.2.1 1 简介 实例化布局的XML文件成相应的View对象.它不能被直接使用,应该使用getLayoutInflater()或getSy ...

  5. View inflate方法和LayoutInflater inflate方法的区别详解

    原创文章,转载请注明出处:http://www.cnblogs.com/baipengzhan/p/6257510.html 我们在Android开发中,对于将布局填充成View对象,最常用的两种办法 ...

  6. LayoutInflater 原理分析 示例

    LayoutInflater简介        LayoutInflater 顾名思义就是布局填充器,做过Android界面编程,相信对这个类都比较熟悉,可能有人说,我们在activity中使用set ...

  7. 获取LayoutInflater对象的方法和inflate方法的一些参数问题

    一.获取LayoutInflater的三种方法 1. LayoutInflater layoutInflater = (LayoutInflater) MainActivity.this.getSys ...

  8. [Android]异步 layout inflation(翻译)

    以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/5829809.html 异步 layout inflation ...

  9. Android Weekly Notes Issue #220

    Android Weekly Issue #220 August 28th, 2016 Android Weekly Issue #220 ARTICLES & TUTORIALS Manag ...

随机推荐

  1. SQL Server2005修改计算机名后不能发布订阅

    在一台安装有SQL Server2005的计算机上,更改计算机名后,在发布订阅的时候提示如下错误报告: 由于需要需要配置一个发布订阅,可是一直报告:" sql server 复制需要有实际的 ...

  2. seleenium与Vs结合

    Vs 自带自动化测试录制工具.selenium则是开源的大众工具.在使用发现vs 自带的工具录制方便,但是修改很难.但是可以提供后续的BUG管理和邮件通知,自动构建等功能.selenium在.net平 ...

  3. 监控系统-mod-gearman

    doc http://labs.consol.de/nagios/mod-gearman/ 安装 yum -y install gearmand-server-0.33-2.rhel6.x86_64. ...

  4. Python基础学习之字符串(1)

    字符串 由字符组成的序列,即字符串. 1.基本字符串操作 所有标准的序列操作(索引.切片.乘法.判断成员资格.求长度.取最小值和最大值)对字符串同样适用: >>> website=' ...

  5. 笨办法学Python(五)

    习题 5: 更多的变量和打印 我们现在要键入更多的变量并且把它们打印出来.这次我们将使用一个叫“格式化字符串(format string)”的东西. 每一次你使用 " 把一些文本引用起来,你 ...

  6. des的根据key进行加密和解密方法

    DES加密: public static string DESEncode(string content, string key) { DESCryptoServiceProvider des = n ...

  7. testng失败重跑

    重跑失败场景 1.要添加两个文件 背景:因为这里只是想单独展示失败的重跑的案例,所以先暂时把app这块的运行注释掉,只跑一个简单的demo,就一个简单类,类中就3个测试方法,失败重跑的原理是,运行方法 ...

  8. AJAX(二):HTTP头部信息

    每个http请求和响应都会带有相应都头部信息,其中有的对开发人员有用,有的页没有什么用默认情况下,发送xhr请求的同时,还有发送下列头部信息 Accept:浏览器能够处理的内容类型 Accept-Ch ...

  9. svn update 时总是提示 Password for '默认密钥' GNOME keyring: 输入密码

    执行svn update 时 总是提示输入密码. $ cd ~/.gnome2/keyrings/ $ ls $ rm 默认密钥.keyring

  10. js控制select选中显示不同表单内容

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...