它的作用类似于findViewById, 不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例化,而findViewById()是找xml布局文件下的具体Widget控件(如Button, TextView等)。
具体作用
1、对于一个没有被载入或者想动态载入的界面,都需要使用LayoutInflater.inflate()来载入。
2、对于一个已经载入的界面,就可以使用Activity.findViewById()的方法来获得其中的界面元素。
它是一个抽象类。
获得LayoutInflater实例的三种方式
LayoutInflater inflater = getLayoutInflater();//调用Activity的getLayoutInflater()
LayoutInflater inflater = LayoutInflater.from(context);
LayoutInflater inflater = (LayoutInflater)context.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
inflate方法
public View inflate (int resource, ViewGroup root)
public View inflate (int resource, ViewGroup root, boolean attachToRoot)
参数1:想要添加的布局
参数2:想要添加到哪个布局上面
参数3:是否直接添加到第二个参数布局上