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 ...
随机推荐
- JavaScript(一) 对象基础
1.定义类或对象 1.1 混合的构造函数/原型方法 用构造函数定义对象的所有非函数属性,类似于Java的构造方法.用原型方法定义对象的函数属性(方法).这种方法是使用比较广泛的定义类或对象的方法. / ...
- 使用 Satis 搭建私有的 Composer 包仓库
简述 iBrand 产品立项时是商业性质的项目,但是在搭建架构时考虑后续的通用性,因此每个模块都设计成一个 Package,作为公司内部用,因此这些包并不能提交到 packagist.org 上去. ...
- TP5.1:实现分页
前提: (1)为了让分页变得更加好看,我的案例加载了bootstrap和jq的文件,具体操作请参考:http://www.cnblogs.com/finalanddistance/p/9033916. ...
- 显示C++数据的数据类型
#include <typeinfo> using namespace std; ... cout << typeid(d).name() << endl; 其中, ...
- 安卓NDK流程
定义wrap类,声明native函数,加载库 package com.ndk.hello; public class Classs { public native String say_hello() ...
- 【Tim Sweeney】Why C++ for Unreal 4?
The first three generations of the Unreal Engine included a sandboxed scripting language, UnrealScri ...
- WIN7如何在任务栏建立我的电脑的快捷图标
1. 在桌面空白处鼠标右击->新建->快捷方式,在弹出的对话框中输入 %SystemRoot%\explorer.exe /E,::{20D04FE0-3AEA-1069-A2D8-08 ...
- node 上的cookie的签名和解签名
cookie签名的原因是防止别人篡改cookie原本的值,如果这个过程中cookie被改变的话,就会在unsign方法返回false 代码: var cookie = require("co ...
- bzoj4943 [Noi2017]蚯蚓排队
题面:http://www.lydsy.com/JudgeOnline/upload/Noi2017D1.pdf 正解:字符串$hash$. 我在考场上写了个$map$的$hash$被卡成$40$分, ...
- first 和firstordefault的用法 简介
https://www.cnblogs.com/1312mn/p/9212325.html