在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById()。不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例化;而findViewById()是找xml布局文件下的具体widget控件(如Button、TextView等)。 具体作用: 1、对于一个没有被载入或者想要动态载入的界面,都需要使用LayoutInflater.inflate()来载入;

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

LayoutInflater 是一个抽象类,在文档中如下声明:

publicabstractclass LayoutInflater extends Object

获得 LayoutInflater 实例的三种方式

1.LayoutInflater inflater = getLayoutInflater();  //调用Activity的getLayoutInflater()

2.LayoutInflater localinflater =(LayoutInflater)context.getSystemService

                                                (Context.LAYOUT_INFLATER_SERVICE);

3. 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):

publicstatic LayoutInflater from(Context context) {       LayoutInflater LayoutInflater =               (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);       if (LayoutInflater ==null) {           thrownew AssertionError("LayoutInflater not found.");       }       return LayoutInflater;   }

  

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

结论:所以这三种方式最终本质是都是调用的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, booleanattachToRoot)    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 等)。

来源:http://blog.sina.com.cn/s/blog_48a45b950100u8ax.html

关于Inflater的更多相关文章

  1. Android学习---ListView和Inflater的使用,将一个布局文件转化为一个对象

    本文将介绍ListView和Inflater的使用,将接上一篇文章内容. 一.什么是ListView? 在android开发中ListView是比较常用的控件,ListView 控件可使用四种不同视图 ...

  2. Android 采用Layout Inflater创建一个View对象

    接着上文<Android ListViewview入门>,本文使用android的Inflater来实现 在layouyt文件夹中新建一个list_item.xml的文件,添加如下代码: ...

  3. android之inflater用法

    在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById().不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例 ...

  4. android获取inflater

    LayoutInflater作用是将layout的xml布局文件实例化为View类对象. 获取LayoutInflater的方法有如下三种: ? LayoutInflater inflater=(La ...

  5. android inflater 用法

    在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById().不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例 ...

  6. Deflater与Inflater的压缩与解压缩

    原文:Deflater与Inflater的压缩与解压缩 package util; import java.util.Arrays; import java.util.zip.Deflater; im ...

  7. java.util.zip.Deflater 压缩 inflater解压 实例

    原文:java压缩解压缩类实例[转] package com.example.helloworld; import java.io.ByteArrayOutputStream; import java ...

  8. 【项目分析】利用C#改写JAVA中的Base64.DecodeBase64以及Inflater解码

    原文:[项目分析]利用C#改写JAVA中的Base64.DecodeBase64以及Inflater解码 最近正在进行项目服务的移植工作,即将JAVA服务的程序移植到DotNet平台中. 在JAVA程 ...

  9. 180918-JDK之Deflater压缩与Inflater解压

    JDK 压缩与解压工具类 在实际的应用场景中,特别是对外传输数据时,将原始数据压缩之后丢出去,可以说是非常常见的一个case了,平常倒是没有直接使用JDK原生的压缩工具类,使用Protosutff和K ...

随机推荐

  1. Chapter 1 First Sight——11

    I didn't relate well to people my age. 我没有向人们叙述清楚我的年龄 Maybe the truth was that I didn't relate well ...

  2. ImageLoader的使用

    显示本地图片 //ImageLoader使用前必须初始化 ImageLoader imageLoader = ImageLoader.getInstance(); imageLoader.init(I ...

  3. jq实现点击按钮后倒计时,多用于手机验证

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. JSP内置对象--pageContext对象(非常重要!!!)

    pageContext对象是javax.servlet.jsp.PageContext类的实例,只要表示的是一个jsp页面的上下文,而且功能强大,几乎可以操作各种内置对象. >forward(S ...

  5. hbase port

    From "Jonathan Hsieh (JIRA)" <j...@apache.org> Subject [jira] [Updated] (HBASE-10123 ...

  6. myeclipse中打开java文件中文乱码

    中文乱码肯定是编码与解码不一样导致. 1.如果是平时写代码都没有问题,但是打开其他项目时出现这种问题: window->preferences->General->Content T ...

  7. ural1542 Autocompletion

    Autocompletion Time limit: 2.0 secondMemory limit: 64 MB The Japanese are infinitely in love with ma ...

  8. JAVA的RSS处理

    一:什么是RSS RSS(really simple syndication) :网页内容聚合器.RSS的格式是XML.必须符合XML 1.0规范. RSS的作用:订阅BLOG,订阅新闻二:RSS的历 ...

  9. [转] Linux下移动virtualbox虚拟硬盘丢失eth0

    1.遇到什么的问题(What)      在新的virtualbox虚拟机上挂上曾使用过的虚拟硬盘,在启动的时候,发现找不到网卡eth0, 在输入ifconfig –a的时候,也没有任何Ethnet的 ...

  10. Divisor Summation_

    Divisor Summation Problem Description Give a natural number n (1 <= n <= 500000), please tell ...