Android attachToRoot对于LayoutInflater.inflate返回视图的影响

132 阅读1分钟

View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot)方法为加载视图时常用的方法,但是当attachToRoot值为true当时候返回当时root,否则为resource对应当视图。 关键代码在 View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot)方法开始部分:

View result = root;

以及结束部分:

// Decide whether to return the root that was passed in or the
// top view found in xml.
if (root == null || !attachToRoot) {
   result = temp;
}