LayoutInflater

来源:互联网 发布:淘宝充q币怎么没到账 编辑:程序博客网 时间:2024/05/21 10:09
在实际开发种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;   
}  
0 0
原创粉丝点击