351_setFactory解析
来源:互联网 发布:照片转成卡通图片软件 编辑:程序博客网 时间:2024/06/05 04:53
setFactory解析
阅读鸿洋大神博客笔记
博客地址:http://blog.csdn.net/lmj623565791/article/details/51503977
先看看Inflator的setFactory和setFactory2
new LayoutInflater().setFactory();
new LayoutInflater().setFactory2();
/**
* Attach a custom Factory interface for creating views while using
* this LayoutInflater. This must not be null, and can only be set once;
* after setting, you can not change the factory. This is
* called on each element name as the xml is parsed. If the factory returns
* a View, that is added to the hierarchy. If it returns null, the next
* factory default {@link #onCreateView} method is called.
*
* <p>If you have an existing
* LayoutInflater and want to add your own factory to it, use
* {@link #cloneInContext} to clone the existing instance and then you
* can use this function (once) on the returned new instance. This will
* merge your own factory with whatever factory the original instance is
* using.
*/
public void setFactory(Factory factory) {
if (mFactorySet) {
throw new IllegalStateException("A factory has already been set on this LayoutInflater");
}
if (factory == null) {
throw new NullPointerException("Given factory can not be null");
}
mFactorySet = true;
if (mFactory == null) {
mFactory = factory;
} else {
mFactory = new FactoryMerger(factory, null, mFactory, mFactory2);
}
}
/**
* Like {@link #setFactory}, but allows you to set a {@link Factory2}
* interface.
*/
public void setFactory2(Factory2 factory) {
if (mFactorySet) {
throw new IllegalStateException("A factory has already been set on this LayoutInflater");
}
if (factory == null) {
throw new NullPointerException("Given factory can not be null");
}
mFactorySet = true;
if (mFactory == null) {
mFactory = mFactory2 = factory;
} else {
mFactory = mFactory2 = new FactoryMerger(factory, factory, mFactory, mFactory2);
}
}
居然几乎一模一样
setFactory2是在SDK>=11以后引入的
有一个类直接就实现了兼容
LayoutInflatorCompat,也是setFactory方法,来看看
/**
* Attach a custom Factory interface for creating views while using
* this LayoutInflater. This must not be null, and can only be set once;
* after setting, you can not change the factory.
*
* @see LayoutInflater#setFactory(android.view.LayoutInflater.Factory)
*/
public static void setFactory(LayoutInflater inflater, LayoutInflaterFactory factory) {
IMPL.setFactory(inflater, factory);
}
来用一下
参数是LayoutInflater inflater, LayoutInflaterFactory factory
LayoutInflaterCompat.setFactory(LayoutInflater.from(this), new LayoutInflaterFactory() {
@Override
public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
Log.e(TAG, "name = " + name);
int n = attrs.getAttributeCount();
for (int i = 0; i < n; i++)
{
Log.e(TAG, attrs.getAttributeName(i) + " , " + attrs.getAttributeValue(i));
}
return null;
}
});
我们打印一下name,再打印一下attrs里面获取的attributeName
注意 super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
要放在下面,不然会报错
打印结果是:
MainActivity﹕ name = TextView
MainActivity﹕ layout_width , -2
MainActivity﹕ layout_height , -2
MainActivity﹕ text , @2131099670
我们试试把TextView换成Button
if (name.equals("TextView"))
{
Button button = new Button(context, attrs);
return button;
}
0 0
- 351_setFactory解析
- 解析
- 解析
- 解析
- 解析
- 解析
- 解析
- 解析
- 解析
- 解析
- 解析[ ]
- 解析
- ffmpeg解析-----解析文件格式
- Xen解析 netfilter解析
- JSON解析,XML解析
- 硬解析 软解析
- 解析xml dom 解析
- JSON解析、XML解析
- Spring学习笔记(一)Spring入门
- Atitit 信用卡与会员卡(包括银行卡)的发展之路
- C语言字符串相关函数
- 解决ScrollView嵌套viewpager冲突问题
- 代价函数
- 351_setFactory解析
- Atitit 开发2d游戏的技术选型attilax总结
- 2016/10/19 牛客网测试学姐的面经 网易+中兴
- 证明数组是回文数组
- 跨程序共享数据,探究内容提供器
- Android应用市场发布
- ADC和DMA
- gulp常用插件-gulp-uglify
- 语句和运算符