Android LayoutInflater 源码分析及个人总结
来源:互联网 发布:淘宝怎样刷好评 编辑:程序博客网 时间:2024/06/06 17:06
我们经常用到的布局解析器LayoutInflater,主要在ListView或者RecycleView的Adapter中,用两个常用方法
第一个方法是 LayoutInflater.from(context).inflate(resource, root);
第二个方法是LayoutInflater.from(context).inflate(resource, root, attachToRoot);
第一个参数resource是布局,第二个root是父布局,第三个参数是决定是否把执行root.add(resource);
这两个方法用一下四种用法:
LayoutInflater.from(context).inflate(R.layout.main_activity, null);LayoutInflater.from(context).inflate(R.layout.main_activity, android.R.id.content);LayoutInflater.from(context).inflate(R.layout.main_activity, android.R.id.content, false);LayoutInflater.from(context).inflate(R.layout.main_activity, android.R.id.content, true);
先上LayoutInflater的inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot)方法源码
public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) { synchronized (mConstructorArgs) { Trace.traceBegin(Trace.TRACE_TAG_VIEW, "inflate"); final Context inflaterContext = mContext; final AttributeSet attrs = Xml.asAttributeSet(parser); Context lastContext = (Context) mConstructorArgs[0]; mConstructorArgs[0] = inflaterContext; 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, inflaterContext, attrs, false); } else { // Temp is the root view that was found in the xml final View temp = createViewFromTag(root, name, inflaterContext, attrs); 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 against its context. rInflateChildren(parser, temp, attrs, 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 (Exception 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; } }
由此可见
使用第一种方式是会在infalte中调用View temp = createViewFromTag(root, name, inflaterContext, attrs),但不会temp.setLayoutParams(params),所以temp;
的layoutParams为空,这是解析出来的布局的大小会有一些问题,当你在需要解析的布局中的根布局设置android:layout_width,android:layout_height不会生效;
第二种方式和第四种方式相同,会执行root.addView(temp, params),并返回root;
第三种方式,也会直接返回temp,但是会执行temp.setLayoutParams(params),这时在根布局设置android:layout_width,android:layout_height会生效。
- Android LayoutInflater 源码分析及个人总结
- Android LayoutInflater源码分析及使用(一)
- Android LayoutInflater源码分析及使用(二)
- Android LayoutInflater源码分析及使用(三)
- Android LayoutInflater.inflate的使用及源码分析
- LayoutInflater效率分析及源码跟踪
- LayoutInflater.inflater()方法及参数源码分析
- Android View 源码 分析 之 LayoutInFlater
- Android面试题-LayoutInflater源码分析
- LayoutInflater源码分析
- LayoutInflater源码分析
- LayoutInflater 源码分析
- 源码分析之LayoutInflater
- 源码分析之LayoutInflater
- LayoutInflater 源码分析
- LayoutInflater源码分析
- LayoutInflater源码分析
- Android LayoutInflater源码解析
- Android动画学习(一)——Android动画系统框架简介
- 乘法表
- 深度学习点滴---处理技巧
- 将博客搬至CSDN
- google 的autodraw
- Android LayoutInflater 源码分析及个人总结
- java输出实心菱形
- Nuttx 工作队列 work queue
- Android动画学习(二)——Tween Animation
- 2011年09月05日
- 详谈String类
- 区域内的人数预测算法对比实验分析
- 欢迎使用 MWeb
- 详谈StringBuffer