1、对于一个没有被载入或者想要动态载入的界面,都需要使用LayoutInflater.inflate()来载入;
2、对于一个已经载入的界面,就可以使用Activiyt.findViewById()方法来获得其中的界面元素。
获得 LayoutInflater 实例的三种方式
1.LayoutInflater infalte=context.getSytemService(Context.LAYOUT_INFLATE_SERVICE);
2.LayoutInflater inflater = getLayoutInflater(); //调用Activity的getLayoutInflater()
3.LayoutInflater flater = LayoutInflater.from(this);
通俗的说,inflate就相当于将一个xml中定义的布局找出来.
因为在一个Activity里如果直接用findViewById()的话,对应的是setConentView()的那个layout里的组件.
View view = flater.inflate(R.layout.example, null);注意空指针,加判断
inflate.inflate(int resource, ViewGroup root, boolean attachToRoot)
如果attachToRoot为false,就表示root只是提供个控件布局参数;如果attachToRoot是true,把这个view解析挂载到root中,返回这个root,;
如果root是null,返回值也是view,是xml的根节点。