以前不知道LayoutInflater的作用和用法,通过最近的学习,对LayoutInflater有了一定的了解,做个笔记。
LayoutInflater的作用是将layout的xml布局文件实例化为View类对象。
具体的代码:
LayoutInflater inflater = LayoutInflater.from(context);
View layout = inflater.inflate(R.layout.main, null);//这时的所定义的layout的类型就是view类
具体的用处(举2个例子):
(1)要自定义一个adapter就要继承于BaseAdapter。而BaseAdapter类中的getView()方法,要对Listview的显示做优化就要用到View类。
这时就要把layout的xml布局文件实例化为View类对象。
View convertView=inflater.inflate(R.layout.list,null);
(2)inflate可以把Layout布局实例化成一个以view类实现成的对象,有需要时再通过setContentView(view)显示出来。