Android LayoutInflater

inflate方法:

public View inflate (int resource, ViewGroup root)
public View inflate (int resource, ViewGroup root, boolean attachToRoot)

两个参数内部调用三个参数

所以一共就四种方法:

inflater.inflate(R.layout.item, null, true);
inflater.inflate(R.layout.item, null, false);
inflater.inflate(R.layout.item, parent, true);
inflater.inflate(R.layout.item, parent, false);
  • 第一个和第二个效果一样,会使R.layout.item里面的根布局宽高失效,返回值ID是R.layout.item里面的根布局的
  • 第三种方法会直接添加到parent,执行addView()可能会报错,返回值是parent的ID
  • 第四种方法(推荐),表示我们会自己进行addview,返回值是R.layout.item里面的根布局的ID

版权声明:本文为sendidtx原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。