关于LayoutInflate详细解释
2015-10-10 14:21
417 查看
今天在项目中用到了LayoutInflate,出现了一些问题,才知道自己对LayoutInflate并不是真正的了解,我所知道的只是用它去加载一个新的布局,其他的就不知道了,LayoutInflate的作用远不止于此
LayoutInflate有四种调用方式
下面从源码的角度进行分析
LayoutInflate有四种调用方式
<span style="white-space:pre"> </span>public View inflate(int resource, ViewGroup root) public View inflate(int resource, ViewGroup root, boolean attachToRoot) public View inflate(XmlPullParser parser, ViewGroup root) public View inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot)我们平时用的最多的就是第一种,把xml文件转化为view用来加载一个新的布局,如果你在一个activity中想如外再加载一个布局,是没有任何问题的,如果你自定义一个view,比如LineLayout、FrameLayout等等,在类里面加载一个布局用第一种方法的话就会出错,
下面从源码的角度进行分析
public View inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot) { synchronized (mConstructorArgs) { Trace.traceBegin(Trace.TRACE_TAG_VIEW, "inflate"); final AttributeSet attrs = Xml.asAttributeSet(parser); Context lastContext = (Context)mConstructorArgs[0]; mConstructorArgs[0] = mContext; View result = root; try { // Look for the root node. int type; while ((type = parser.next()) != XmlPullParser.START_TAG && type != XmlPullParser.END_DOCUMENT) { // Empty } if (type != XmlPullParser.START_TAG) { throw new InflateException(parser.getPositionDescription() + ": No start tag found!"); } final String name = parser.getName(); if (DEBUG) { System.out.println("**************************"); System.out.println("Creating root view: " + name); System.out.println("**************************"); } if (TAG_MERGE.equals(name)) { if (root == null || !attachToRoot) { throw new InflateException("<merge /> can be used only with a valid " + "ViewGroup root and attachToRoot=true"); } rInflate(parser, root, attrs, false, false); } else { // Temp is the root view that was found in the xml final View temp = createViewFromTag(root, name, attrs, false); ViewGroup.LayoutParams params = null; if (root != null) { if (DEBUG) { System.out.println("Creating params from root: " + root); } // Create layout params that match root, if supplied params = root.generateLayoutParams(attrs); if (!attachToRoot) { // Set the layout params for temp if we are not // attaching. (If we are, we use addView, below) temp.setLayoutParams(params); } } if (DEBUG) { System.out.println("-----> start inflating children"); } // Inflate all children under temp rInflate(parser, temp, attrs, true, true); if (DEBUG) { System.out.println("-----> done inflating children"); } // We are supposed to attach all the views we found (int temp) // to root. Do that now. if (root != null && attachToRoot) { root.addView(temp, params); } // Decide whether to return the root that was passed in or the // top view found in xml. if (root == null || !attachToRoot) { result = temp; } } } catch (XmlPullParserException e) { InflateException ex = new InflateException(e.getMessage()); ex.initCause(e); throw ex; } catch (IOException e) { InflateException ex = new InflateException( parser.getPositionDescription() + ": " + e.getMessage()); ex.initCause(e); throw ex; } finally { // Don't retain static reference on context. mConstructorArgs[0] = lastContext; mConstructorArgs[1] = null; } Trace.traceEnd(Trace.TRACE_TAG_VIEW); return result; } }源码有点长,我们只看关键的几行代码即可
<span style="white-space:pre"> </span>if (root != null) { if (DEBUG) { System.out.println("Creating params from root: " + root); } // Create layout params that match root, if supplied params = root.generateLayoutParams(attrs); if (!attachToRoot) { // Set the layout params for temp if we are not // attaching. (If we are, we use addView, below) temp.setLayoutParams(params); } }当root不等于null和attachToRoot为false的时候,才会返回视图。
相关文章推荐
- apache和tomcat有什么不同,为什么要整合apache 和tomcat?
- CentOS 6 单网卡安装openstack havana
- VS2003一点查找按钮就卡死处理方法
- windows上设置自动删除oracle归档日志
- 实用工具汇总
- Python微博地点签到大数据实战(一)微博API的使用
- Android 主流分辨率
- 单机与网络性能测试
- PHP Socket基础
- redis类型[string 、list 、 set 、sorted set 、hash]
- 如何判断是否在一个网路中
- HDU 5067-Harry And Dig Machine(DFS)
- 如何查看mac系统是32位还是64位的操作系统
- hdu-3571:N-dimensional Sphere+高斯消元
- qq悬浮窗简单代码分享!
- pl/sql报错:Identifier is too long
- 基于Raphael.js的实现
- mysql修改密码
- C++11新特性
- tf-idf算法实现