LayoutInflater inflater.inflate()方法解释

inflater.inflate一共有4个方法

inflate(@LayoutRes int resource, @Nullable ViewGroup root)
inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot)
inflate(XmlPullParser parser, @Nullable ViewGroup root)
inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot)

上面两个方法就是将解析XML调用下面两个对应的方法。

public View inflate(@LayoutRes int resource, @Nullable ViewGroup root) {
    return inflate(resource, root, root != null);
}
public View inflate(XmlPullParser parser, @Nullable ViewGroup root) {
    return inflate(parser, root, root != null);
}

我们常用的方法就是上面两个。

如果没有root返回的view不会设置LayoutParams

如果有root,attachToRootfalse的话就会将root的LayoutParams设置给view并返回的view

如果有root,attachToRoottrue的话就会将view添加到root中并返回已添加view的root


你可能感兴趣的:(LayoutInflater inflater.inflate()方法解释)