从源码分析LayoutInflate的inflate方法

LayoutInflater的inflate方法
public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot);

resource:要创建布局的xml
root:父视图
attachToRoot:是否将创建布局生成的view添加进父视图中
该方法会继续调用以下代码:

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
L35				final View temp = createViewFromTag(root, name, inflaterContext, attrs);
				ViewGroup.LayoutParams params = null;
L37				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);
L44					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) {
L60					root.addView(temp, params);
				}
				// Decide whether to return the root that was passed in or the
				// top view found in xml.
L65				if (root == null || !attachToRoot) {
L66					result = temp;
				}
			}
		}
		catch (XmlPullParserException e) {
			final InflateException ie = new InflateException(e.getMessage(), e);
			ie.setStackTrace(EMPTY_STACK_TRACE);
			throw ie;
		}
		catch (Exception e) {
			final InflateException ie = new InflateException(parser.getPositionDescription()
			                        + ": " + e.getMessage(), e);
			ie.setStackTrace(EMPTY_STACK_TRACE);
			throw ie;
		}
		finally {
			// Don't retain static reference on context.
			mConstructorArgs[0] = lastContext;
			mConstructorArgs[1] = null;
			Trace.traceEnd(Trace.TRACE_TAG_VIEW);
		}
		return result;
	}
}
第一种情况:

root不为null,attachToRoot为false
从35行代码可以看到,temp为我们传入的布局所生成的View
从37和44行代码可以得知,当root不为null时,attachToRoot为false时,会根据root的布局参数给temp提供LayoutParams的值。
然后从65和66行看到,将temp赋值给返回值result,inflate方法所返回的View为我们所传入布局生成的view,此时view并没有成为root的子view。

第二种情况:

root不为null,attachToRoot为true
从60行代码可以得知,当root不为null且attachToRoot为true时,会把生成的view添加进root视图中,并为view设置布局参数,此时inflate方法返回的View并不是布局所生成的view,而是root视图。

第三种情况:

root为null,attachToRoot为false或true
这种情况,仅会根据所传入的布局生成对应的view,并且不会为该view设置布局参数,inflate方法返回值也是view。

只需要传两个参数的inflate方法

如果root为null,则相当于上述的第3种情况,root不为null,则相当于上述的第2种情况

public View inflate(@LayoutRes int resource, @Nullable ViewGroup root) {
     return inflate(resource, root, root != null);
}
总结
rootattachToRoot返回值view是否添加至rootview是否设置LayoutParams
1!=nullfalseviewfalsetrue
2!=nulltrueroottruetrue
3null/viewfalsefalse
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值