inflate方法参数
总结
方法
- View inflate(@LayoutRes int resource, @Nullable ViewGroup root)
- View inflate(XmlPullParser parser, @Nullable ViewGroup root)
- View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot)
- View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) {
参数
- int resource:需要inflate的布局文件资源id
- ViewGroup root: 这个inflate出来的View的父容器
- boolean attachToRoot:是否将inflate出来的View添加到指定的root父容器中
- XmlPullParser parser:XML解析器
解释
-
root = null
也就是未指定inflate View的父容器,那么肯定无法将View绑定到指定父容器中,从而inflate View也就丢失了其LayoutParams属性,,返回的View也就是最终inflate出来的Veiw
-
root != null, attachToRoot = true
-
root != null, attachToRoot = false
inflate View 会有LayoutParams属性,即在resources布局文件中设置的width与height属性会被设置进来,
true 与 false 的区别:
- true 执行 root.addView(View,params),false不执行
- true 返回的 View 的是 root,false 返回的是 inflate 出来的 View
-
一般项目中需求很少有不需要LayoutParams的场景,故父容器 root 需要指定的,而且在AS lint提示中也能体现出来的,如图:
- 使用RecyclerView 添加header、footer、emptyView时,
这样inflate:view = inflater.inflate(layoutId, viewGroup, false);
inflater.inflate(layoutId, null)会丢失LayoutParams导致渲染出来后布局错乱
如果没有现成的viewgroup可以new一个
源码解析
/**
* Inflate a new view hierarchy from the specified XML node. Throws
* {@link InflateException} if there is an error.
* <p>
* <em><strong>Important</strong></em> For performance
* reasons, view inflation relies heavily on pre-processing of XML files
* that is done at build time. Therefore, it is not currently possible to
* use LayoutInflater with an XmlPullParser over a plain XML file at runtime.
*
* @param parser XML dom node containing the description of the view
* hierarchy.
* @param root Optional view to be the parent of the generated hierarchy (if
* <em>attachToRoot</em> is true), or else simply an object that
* provides a set of LayoutParams values for root of the returned
* hierarchy (if <em>attachToRoot</em> is false.)
* @param attachToRoot Whether the inflated hierarchy should be attached to
* the root parameter? If false, root is only used to create the
* correct subclass of LayoutParams for the root view in the XML.
* @return The root View of the inflated hierarchy. If root was supplied and
* attachToRoot is true, this is root; otherwise it is the root of
* the inflated XML file.
*/
public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) {
...
//初始化返回的View为指定的父容器root
View result = root;
...
final View temp = createViewFromTag(root, name, inflaterContext, attrs);
ViewGroup.LayoutParams params = null;
//从此处的判断可以知道只有root!=null的情况下,才会执行解析inflate View的LayoutParams属性
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);
}
}
// 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;
}
}
网友评论