具体作用:
- 对于一个没有被载入或者想要动态载入的界面,都需要使用LayoutInflater.inflate()来载入;
- 对于一个已经载入的界面,就可以使用Activiyt.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 Resourece,ViewGroup root) 作用:填充一个新的视图层次结构从指定的XML资源文件中 reSource:View的layout的ID root: 生成的层次结构的根视图 return 填充的层次结构的根视图 如果参数root提供了,那么root就是根视图;否则填充的XML文件的根就是根视图。其余几个重载的inflate函数类似。
LayoutInflater inflate = LayoutInflater.from(this);
View view = inflate.inflate(R.layout.main,null);
setContentView(view);