第一种
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
inflater.inflate(...);//有多种构造函数
第二种
View inflate(Context context, int resource, ViewGroup root)
下面是应用在ListView中的效果:
效果一
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
inflater.inflate(R.layout.list_item, null);
inflater.inflate(R.layout.list_item, null,false);
View.inflate(context,R.layout.list_item,null);
这三种写法的效果是一样的,单纯的将布局塞到listview里面,所以根节点的属性并没有被使用,比如layout_width、layout_height以及layout_margin等等:
效果二
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
inflater.inflate(R.layout.list_item, parent, false);
这种写法是给你的布局根节点加了一个根节点,所以你的根节点上的属性都会得到体现。
![](https://img-my.csdn.net/uploads/201412/22/1419224945_8082.png)
其他写法:
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
inflater.inflate(R.layout.list_item, parent, false);
inflater.inflate(R.layout.list_item, parent);
inflater.inflate(R.layout.list_item, parent, true);
View.inflate(context,R.layout.list_item,parent);
这几种写法是一样的效果,不适用于listview,会出错,一般不做使用。