布局填充器(LayoutInflater)

来源:互联网 发布:用js写带框99乘法表 编辑:程序博客网 时间:2024/05/16 07:24
实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于 findViewById()

不同点的是:
LayoutInflater 是用来找layout下xml布局文件,并且实例化!
findViewById() 是找具体xml下的具体 widget控件(如:Button,TextView等)。


一般来讲,我们用LayoutInflater做一件事:inflate。
目的都是把xml表述的layout转化为View。这个类是用来实例化布局的 XML文件到相应的视图对象。它是不能直接使用——使用 getlayoutinflater()或getSystemService (String)来检索一个标准的 LayoutInflater实例已经与当前上下文和为你的设备上运行的正确配置


获取LayoutInflater对象三种方法:

1、LayoutInflater inflater=getLayoutInflater();
2、LayoutInflater inflater=(LayoutInflater)mContext.getSystemServic(LAYOUT_INFLATER_SERVICE);
3、LayoutInflater inflater=LayoutInflater.from(context); //这种方法在重写BaseAdapter时常用


他们实质是一样的,请看源码

public static LayoutInflater from(Context context) {
LayoutInflater LayoutInflater =
(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (LayoutInflater == null) {
throw new AssertionError("LayoutInflater not found.");
}
return LayoutInflater;
}



3 0
原创粉丝点击