美文网首页
LayoutInflater.inflate方法参数

LayoutInflater.inflate方法参数

作者: 卡路fly | 来源:发表于2020-06-03 00:29 被阅读0次

推荐用下边第二种方式:
inflater.inflate(R.layout.item, parent, false);

  1. 如果root为null,无论attachToRoot为true或者false,效果都是一样的
  2. 如果root不为null,attachToRoot为true,表示将layout布局添加到root布局中
  3. 如果root不为null,attachToRoot为false,表示不将layout布局添加到root布局,若要添加则需要手动addView
  4. 如果root不为null,不设置attachToRoot(即调用两个参数的方法),情况和(2)中一样

两个参数

public View inflate (int resource, ViewGroup root)
public View inflate (XmlPullParser parser, ViewGroup root)
  1. 第一个参数是子布局
  2. 第二个参数是要加载进去的父布局

三个参数的

public View inflate (XmlPullParser parser, ViewGroup root, boolean attachToRoot)
public View inflate (int resource, ViewGroup root, boolean attachToRoot)
  1. 第一个参数是子布局
  2. 第二个参数是要加载进去的父布局
  3. 第三个就是是否将载入的视图绑定到根视图中

root != null && attach = false

我们在开发的过程中给控件所指定的layout_width和layout_height属性,表示一个控件在容器中的大小,就是说这个控件必须在容器中,这个属性才有意义,否则无意义。

这就意味着如果直接将linearlayout加载进来而不给它指定一个父布局,则inflate布局的根节点的layout_width和layout_height属性将会失效(因为这个时候linearlayout将不处于任何容器中,那么它的根节点的宽高自然会失效)。

如果想让linearlayout的根节点有效,又不想让其处于某一个容器中,那我就可以设置root不为null,而attachToRoot为false。这样,指定root的目的也就很明确了,即root会协助linearlayout的根节点生成布局参数,只有这一个作用。


来源: LayoutInflater.inflate方法的参数问题

相关文章

网友评论

      本文标题:LayoutInflater.inflate方法参数

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