在使用自定义listView和viewAdapter的时候使用到了LayoutInflater,现在简单记录一下
LayoutInflater的作用
LayoutInflater是布局填充器,例如我们需要把主布局中添加一个view,就可以使用LayoutInflater加载view所在的view.xml文件,然后添加到主布局中
获取LayoutInflater的三种方式
1.
LayoutInflater layoutInflater = getLayoutInflater();//获取Activity的LayoutInflater
2.
LayoutInflater layoutInflater = LayoutInflater.from(context);
3.LayoutInflater layoutInfalter = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
使用inflater填充布局
inflater 的原型:
inflate(int resource, ViewGroup root, boolean attachToRoot)
1.inflater(resource,root) <==> inflater(resource,root,true)
2.inflater(resource,null) <==> inflater(resource,root,false)
先来分析第一种写法,是把resource资源添加到主布局对象root中。
第二种是把resource布局资源加载,但不把resource布局添加到父布局。
使用例子见参考文章
参考文章:http://kaywu.xyz/2015/04/05/LayoutInfalter/
参考文章:http://blog.csdn.net/guolin_blog/article/details/12921889