[原文参考http://lpqsun-126-com.iteye.com/blog/1158070]
Inflater英文意思是膨胀,在android中应该是扩展的意思吧。
LayoutInflater的作用类似于 findViewById(),不同点是LayoutInflater是用来找layout文件夹下的xml布局文件,并且实例化!而 findViewById()是找具体某一个xml下的具体 widget控件(如:Button,TextView等)。
具体作用:
1、对于一个没有被载入或者想要动态载入的界面,都需要使用LayoutInflater.inflate()来载入;

2、对于一个已经载入的界面,就可以使用Activiyt.findViewById()方法来获得其中的界面元素。

获得 LayoutInflater 实例的三种方式:

LayoutInflater inflater = getLayoutInflater();  //调用Activity的getLayoutInflater()  LayoutInflater localinflater =  (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  LayoutInflater inflater = LayoutInflater.from(context);       


其实,这三种方式本质是相同的,从源码中可以看出:

getLayoutInflater():

Activity 的 getLayoutInflater() 方法是调用 PhoneWindow 的getLayoutInflater()方法,看一下该源代码:

 public PhoneWindow(Context context) {            super(context);            mLayoutInflater = LayoutInflater.from(context);    }    

可以看出它其实是调用 LayoutInflater.from(context)。

LayoutInflater.from(context):



 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;     }   



可以看出它其实调用 context.getSystemService()。

inflate 方法
通过 sdk 的 api 文档,可以知道该方法有以下几种过载形式,返回值均是 View 对象,如下:
 public View inflate (int resource, ViewGroup root)    public View inflate (XmlPullParser parser, ViewGroup root)        public View inflate (XmlPullParser parser, ViewGroup root, boolean attachToRoot)        public View inflate (int resource, ViewGroup root, boolean attachToRoot)   


1:
public View inflate (int resource, ViewGroup root)
reSource:View的layout的ID
root:如果为null,则将此View作为根,此时既可以应用此View中的其他控件了。
如果!null, 则将默认的layout作为View的根。

2:
public View inflate ( XmlPullParser parser, ViewGroup root)
parser:你需要解析xml的解析接口
root:如果null,则将此View作为根,此时既可以应用此View中的其他控件了。
如果!null, 则将默认的layout作为View的根。

3:
public View inflate ( XmlPullParser parser, ViewGroup root, boolean attachToRoot)
parser:你需要解析View的xml的解析接口
root:如果null,则将此View作为根,此时既可以应用此View中的其他控件了。
如果!null, 则将默认的layout作为View的根。
attachToRoot:
ture:也就将此解析的xml作为View根
fase:则为默认的xml,做为根视图View

4:
public View inflate (int resource, ViewGroup root, boolean attachToRoot)
resource:View的layout的ID

root:如果null,则将此View作为根,此时既可以应用此View中的其他控件了。

如果!null, 则将默认的layout作为View的根。

attachToRoot:

ture:也就将此解析的xml作为View根
fase:则为默认的xml,做为根视图View

findViewById
R.layout.xx 是引用res/layout/xx.xml的布局文件(inflate方法),R.id.xx是引用布局文件里面的组件,组件的id是xx...(findViewById方法)。看看R.java配置文件吧,R对文件分类管理,多写几个layout.xml后你会发现,所有的组件id都能用R.id.xx来查看,但是组件不在setContentView()里面的layout中就无法使用,Activity.findViewById()会出现空指针异常 。
他有二中表现形式
1.Activity.findViewById 发现xml定义的ID
2.view.findViewById 视图的子对象

更多相关文章

  1. Android中使用Streams的两种方法
  2. 我的Android进阶之旅------>Ubuntu下不能识别Android设备的解决
  3. 动态修改Android参数信息的方法绕过改机检测
  4. android 横竖屏限制的配置方法
  5. Android学习——基础组件
  6. Android下拉列表控件spinner-andoid学习之旅(十一)

随机推荐

  1. Android A/B System OTA分析(三)主系统和bo
  2. [转]android 安全与权限(Security and Pe
  3. 2013.04.08——— android 关于部分文字
  4. android UI 单线程模型
  5. Tablayout属性以及设置行间距和列间距
  6. GAE和android的几个中文问题
  7. android控件的监听绑定方法
  8. Android(安卓)自定义View 标识当前选中的
  9. android 日历开发附源码(附源码)
  10. 在Mer系统中启动Android系统(一)