参考该博客:http://www.cnblogs.com/top5/archive/2012/05/04/2482328.html

LayoutInflater是一个抽象类,通过调用其实例方法inflate(),将res/layout下的xml布局文件进行实例化,不同于findvirebyid()是找布局文件下的控件进行实例。

1、对于一个没有被载入或者想要动态载入的界面,都需要使用LayoutInflater.inflate()来载入;

2、对于一个已经载入的界面,就可以使用Activiyt.findViewById()方法来获得其中的界面元素。
 
LayoutInflater是一个抽象类,通过三种方法可以获得其对象。
1. LayoutInflater inflater = getLayoutInflater();//调用Activity的getLayoutInflater()

2. LayoutInflater inflater = LayoutInflater.from(context);  
3. LayoutInflater inflater =  (LayoutInflater)context.getSystemService
                              (Context.LAYOUT_INFLATER_SERVICE);
 

三种方法的本质是相同的,都是调用的Context.getSystemService()。

另外getSystemService()是Android很重要的一个API,它是Activity的一个方法,根据传入的NAME来取得对应的Object,然后转换成相应的服务对象。

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)
示意代码:

LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);       
View view = inflater.inflate(R.layout.custom, (ViewGroup)findViewById(R.id.test));       
//EditText editText = (EditText)findViewById(R.id.content);// error 
EditText editText = (EditText)view.findViewById(R.id.content);
对于上面代码,指定了第二个参数 ViewGroup root,当然你也可以设置为 null 值。

注意:
·inflate 方法与 findViewById 方法不同;
·inflater 是用来找 res/layout 下的 xml 布局文件,并且实例化;
·findViewById() 是找具体 xml 布局文件中的具体 widget 控件(如:Button、TextView 等)。
 
 
 
 
 
 
 

对layoutInflater的理解的更多相关文章

  1. [译转]深入理解LayoutInflater.inflate()

    原文链接:https://www.bignerdranch.com/blog/understanding-androids-layoutinflater-inflate/ 译文连接:http://bl ...

  2. Android LayoutInflater.inflate(int resource, ViewGroup root, boolean attachToRoot)的参数理解

    方法inflate(int resource, ViewGroup root, boolean attachToRoot) 中 第一个参数传入布局的资源ID,生成fragment视图,第二个参数是视图 ...

  3. 从源码角度深入理解LayoutInflater

    关于LayoutInflater,在开发中经常会遇到,特别是在使用ListView的时候,这个几乎是必不可少.今天我们就一起来探讨LayoutInflater的工作原理. 一般情况下,有两种方式获得一 ...

  4. 深入理解 Android 之 View 的绘制流程

    概述 本篇文章会从源码(基于Android 6.0)角度分析Android中View的绘制流程,侧重于对整体流程的分析,对一些难以理解的点加以重点阐述,目的是把View绘制的整个流程把握好,而对于特定 ...

  5. 对于Fragment的一些理解

    前言 Fragment想必大家不陌生吧,在日常开发中,对于Fragment的使用也很频繁,现在主流的APP中,基本的架构也都是一个主页,然后每个Tab项用Fragment做布局,不同选项做切换,使用起 ...

  6. 转载《 LayoutInflater 的inflate函数用法详解》

    很多人在网上问LayoutInflater类的用法,以及inflate()方法参数的含义,现解释如下: inflate()的作用就是将一个用xml定义的布局文件查找出来,注意与findViewById ...

  7. Android LayoutInflater原理分析,带你一步步深入了解View(一)

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/12921889 有不少朋友跟我反应,都希望我可以写一篇关于View的文章,讲一讲Vi ...

  8. 【IOS】从android角度来实现(理解)IOS的UITableView

    以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/3403124.html   本人从在学校开始到现在上班(13年毕 ...

  9. View.inflate和LayoutInflater的inflate方法区别

    平时ListView加载item中,adapter的getView方法中,我们经常用到: LayoutInflater.from(mContext).inflate(R.layout.it ,pare ...

随机推荐

  1. Angularjs集成第三方js插件之Uploadify

    有时候需要用一些第三方插件,比如datepicker,slider,或者tree等.以前的做法是直接通过jquery取得某个元素,然后调用某个方法即可.但在angularjs中,不能直接这么写,必须写 ...

  2. Spring Security构建Rest服务-0800-Spring Security图片验证码

    验证码逻辑 以前在项目中也做过验证码,生成验证码的代码网上有很多,也有一些第三方的jar包也可以生成漂亮的验证码.验证码逻辑很简单,就是在登录页放一个image标签,src指向一个controller ...

  3. java验证码工具

    工具类: package com.lhy.web.servlet; import java.awt.BasicStroke; import java.awt.Color; import java.aw ...

  4. Notepad++软件的下载与安装步骤(图文详解)

    不多说,直接上干货! 这款软件非常好用!!!   1.下载Notepad++软件 Notepad++非常好用,想要安装首先我们要下载(废话)~ 百度搜索“Notepad++”直接就可以找到主页: 主页 ...

  5. ES6-Set and Map

    依赖文件地址 :https://github.com/chanceLe/ES6-Basic-Syntax/tree/master/js <!DOCTYPE html> <html&g ...

  6. C#的Lambda表达式嵌套例子

    /* *curStatsResult是List<string>类型, *x.GetAllOsVersion()结果是string[]类型, *这里是先使用SelectMany()返回一个结 ...

  7. Ruby语言学习系列--String 类函数

        函数名称 说明 示例 * 将字符串拷贝N次 “ha”*4    >> “hahahaha” + <<  concat 连接字符串 “yes” + “no”  >& ...

  8. vuex中怎么把‘库’中的状态对象赋值给内部对象(三种方法)

    一.通过computed的计算属性直接赋值 import store from '@/store/store' export default{ name: 'count', data(){ retur ...

  9. < Python Index >

    1. 基本语法   1.1 常量/变量   1.2 运算符   1.3 表达式   1.4 程序结构 2. 内置数据结构    2.1 列表    2.2 元组    2.3 集合    2.4 st ...

  10. 使用 Selenium 实现基于 Web 的自动化测试

    (转自http://www.ibm.com/developerworks/cn/web/1209_caimin_seleniumweb/index.html) Selenium 是一个用于 Web 应 ...