美文网首页
[基础] inflate()

[基础] inflate()

作者: 兔斯基第2号 | 来源:发表于2018-11-25 18:53 被阅读0次

inflate

参见:inflate()方法详解和源码分析
方法一:
public View inflate(int resource, ViewGroup root) {}

方法二:
public View inflate(int resource, ViewGroup root, boolean attachToRoot) {}

参数:
resource:需要实例化的布局资源id。
root:ViewGroup类型视图组对象
attachToRoot:是否将resource实例化后的View添加到参数root中。
返回值:
如果root为空,直接返回resource实例化后的View对象;
如果root不为空,attachToRoot为true,将resource实例化为view对象,忽略view的最外层视图在xml布局中定义的属性,将view添加到root中,并将root返回。
如果root不为空,attachToRoot为false,将resource实例化为view对象,为view的最外层视图设置其在xml布局中定义的属性,并将view对象返回。


两个参数的方法实际上调用的是三个参数的方法:

public View inflate(int resource, ViewGroup root) {
    return inflate(resource, root, root != null);

inflate(R.layout.xxx, rootView)等同于inflate(R.layout.xxx, rootView, true)
这句话的效果是实例化布局,并把得到的View加入到rootView中去。所以如果只是实例化一个布局而不添加到rootView,就设为false,inflate(R.layout.xxx, rootView, false)

相关文章

网友评论

      本文标题:[基础] inflate()

      本文链接:https://www.haomeiwen.com/subject/ngxmqqtx.html