[原文参考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(安卓)setContentView方法解析(一)
  2. Android中使用PULL方式解析XML文件深入介绍
  3. Android学习系列(40)--Android主题和样式之系统篇(下)
  4. Android学习之使用RadioGroup与RadioButton实现单选效果
  5. Android(安卓)ApiDemos示例解析(15):App->Activity->SetWallpaper
  6. Android利用setLayoutParams在代码中调整布局(Margin和居中)
  7. Android快速开发工具集合——YUtils
  8. Android(安卓)SafeMode 解析
  9. Android(安卓)时间显示控件 TextClock

随机推荐

  1. Android(安卓)P应用适配指南
  2. Android系统五大布局详解Layout
  3. 超简单Android(安卓)Studio导入第三方库(
  4. 还不知道 Jetpack,你可能赶不上Android开
  5. 一篇读懂:Android/iOS手机如何通过音频接
  6. Android可收缩/扩展的TextView【1】
  7. 如何快速步入Android(安卓)开发之旅
  8. [一步一步破解apk]破解1518占卜
  9. 如何查看Android设备上的分区信息
  10. Android(安卓)内置群组,联系人