public View inflate (int resource, ViewGroup root, boolean attachToRoot)
我们在使用这个方法时,要清楚原理,下面是这个方法的文档介绍:
Inflate a new view hierarchy from the specified xml resource. Throws InflateException if there is an error.
Parameters
resource
ID for an XML layout resource to load (e.g., R.layout.main_page)
root
Optional view to be the parent of the generated hierarchy (if attachToRoot is true), or else simply an object that provides a set of LayoutParams values for root of the returned hierarchy (if attachToRoot is false.)
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.
Returns
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.
意思就是:root参数表示,如果attachToRoot为true的话,那么root为这个resource布局的父布局。如果attachToRoot为false,那么root仅仅为打气的这个布局提供了一组LayoutParams,暂时可以理解为所有的布局都需要一个LayoutParams,所以必须提供.
其原理目前我还没有完全理解,基本可以总结为,在Fragment的OnCreateView方法中,需要用这个 View view = inflater.inflate(R.layout.my_fragment_layout, container, false);,因为Fragment的布局本身就是根布局
在自定义ViewGroup中,要用这个
//把生成的布局当做子类加入到当前控件中
//this表示当前这个自定义ViewGroup
View view = LayoutInflater.from(context).inflate(R.layout.mapmodel_relat_view, this, true);