在实际开发中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, 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 等)。

android inflater 用法的更多相关文章

  1. android——inflater 用法(转)

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

  2. Android Fragment用法详解(2)--动态添加Fragment

    在上一篇文章<Android Fragment用法详解(1)--静态使用Fragment>我们讲解了Fragment的最简单的用法.这次我们来说一说Fragment复杂一丢丢的用法.在代码 ...

  3. Android Meun 用法

    Android Meun 用法 点击菜单实体键弹出菜单:如下图 main_activity.xml <?xml version="1.0" encoding="ut ...

  4. Android ViewPager 用法

    Android ViewPager 用法 场景:一般第一次打开应用程序时,程序会有一个提示页来给展现应用程序都有哪些功能:或者程序更新时,又更新哪些新特性,都可以使用ViewPager Demo 描述 ...

  5. Android Intent 用法全面总结

    [代码全屏查看]-Android Intent 用法全面总结 // [1].[代码] 调用拨号程序 跳至 [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] / ...

  6. Android GLSurfaceView用法详解(二)

    输入如何处理       若是开发一个交互型的应用(如游戏),通常需要子类化 GLSurfaceView,由此可以获取输入事件.下面有个例子: java代码: package eoe.ClearTes ...

  7. Android ScrollView用法

    Android ScrollView用法 今天试着使用了一下Android的滚轮,以下是一个小小的测试,读取测试文件,主要是使用scrollTo函数和getScrollY(),程序点击BUTTON按钮 ...

  8. Android Service用法知识点的讲解

    Android Service 学习Service相关知识点: android service 的基础知识,生命周期,service分类,运行地点(本地服务,远程服务),运行类型(前台服务,后台服务) ...

  9. Android Fragment用法知识点的讲解

    Android Fragment用法的讲解 碎片,它的出现是为了更好展示UI的设计,让程序更加得到充分的展示.Fragment的出现,如微信的额主界面包含多个Fragment,使得微信功能更加简洁明了 ...

随机推荐

  1. ThinkPHP3.2.3验证码显示、刷新、校验

    显示验证码 首先在Home/Controller下创建一个公共控制器PublicController <?php namespace Home\Controller; use Think\Con ...

  2. 弱安全协议探测工具-sslciphercheck

    SSL(Secure Sockets Layer 安全套接层),及其继任者传输层安全(Transport Layer Security,TLS)是为 网络通信提供安全及数据完整性的一种安全协议.TLS ...

  3. 转:HTTP请求(GET、POST和soap区别)和响应

    一直对Http请求和SOAP请求不是太理解,只是知道SOAP是基于Http的,并且增加了很多XML标签,SOAP经常用在WebService中,比如在C#中创建一个WebService,然后在客户端生 ...

  4. JavaScript中的自调用函数

    自调用函数 自调用函数(self-invoking funciton)就是函数定义完之后会立即运行的函数. 最常见的写法是: (function() { // function body... }() ...

  5. IOS开发中单例模式使用详解

    第一.基本概念 单例模式是一种常用的软件设计模式.在它的核心结构中只包含一个被称为单例类的特殊类.通过单例模式可以保证系统中一个类只有一个实例而且该实例易于外界访问. 第二.在IOS中使用单例模式的情 ...

  6. Spring AOP实现方式三【附源码】

    注解AOP实现 源码结构: 1.首先我们新建一个接口,love 谈恋爱接口. package com.spring.aop; /** * 谈恋爱接口 * * @author Administrator ...

  7. poj1947Rebuilding Roads(树形DP)

    链接 刚接触 树上背包..有点抽象化 找好父亲和儿子的关系 及状态转移方程 代码里有详细的注释  就不解释了 #include <iostream> #include<cstdio& ...

  8. c程序设计语言_习题8-4_重新实现c语言的库函数fseek(FILE*fp,longoffset,intorigin)

      fseek库函数 #include <stdio.h> int fseek(FILE *stream, long int offset, int origin); 返回:成功为0,出错 ...

  9. Cocos2d-x ios 下http请求的另一种实现

    简单描述下需求:游戏要加入事件log,比如玩家升到10级:创建角色:或是,触发这些事件后要求客户端忘后台抛送一条log信息.一般情况下,我们可以直接使用cocos自带的HttpClient(底层用li ...

  10. Android 单字阅读

    package com.desmand.screencapture; import android.app.Activity; import android.content.Intent; impor ...