获得 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()方法,看一下该源代码:

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

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

LayoutInflater.from(context):

1 public static LayoutInflater from(Context context) {   2     LayoutInflater LayoutInflater =   3             (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);   4     if (LayoutInflater == null) {   5         throw new AssertionError("LayoutInflater not found.");   6     }   7     return LayoutInflater;   8 } 

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

结论:所以这三种方式最终本质是都是调用的Context.getSystemService()。

LayoutInflater的实例化的更多相关文章

  1. LayoutInflater和inflate()方法的用法

    LayoutInflater作用是将layout的xml布局文件实例化为View类对象. 实现LayoutInflater的实例化共有3种方法, (1).通过SystemService获得 Layou ...

  2. Android LayoutInflater&LayoutInflaterCompat源码解析

    本文分析版本: Android API 23,v4基于 23.2.1 1 简介 实例化布局的XML文件成相应的View对象.它不能被直接使用,应该使用getLayoutInflater()或getSy ...

  3. LayoutInflater和inflate的用法,有图有真相

    1.概述 有时候在我们的Activity中用到别的layout,并且要对其组件进行操作,比如: A.acyivity是获取网络数据的,对应布局文件为A.xml,然后需要把这个数据设置到B.xml的组件 ...

  4. Android群英传笔记——第四章:ListView使用技巧

    Android群英传笔记--第四章:ListView使用技巧 最近也是比较迷茫,但是有一点点还是要坚持的,就是学习了,最近离职了,今天也是继续温习第四章ListView,也拖了其实也挺久的了,list ...

  5. LayoutInflater这个类还是非常有用的,它的作用类似于findViewById()。 但他是找XML文件并实例化

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

  6. Android 中 LayoutInflater 的使用

    一.LayoutInflater 的作用 我们一般使用 LayoutInflater 做一件事:View inflate(int resource, ViewGroup root); inflate( ...

  7. Android LayoutInflater详解

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

  8. LayoutInflater的使用

    在实际工作中,事先写好的布局文件往往不能满足我们的需求,有时会根据情况在代码中自定义控件,这就需要用到LayoutInflater.LayoutInflater在Android中是“扩展”的意思,作用 ...

  9. Android 查缺补漏之Adapter 和 LayoutInflater

    在之前的博客我也讲过 Adapter 和 inflater,但发现讲的不够清楚,好多读者看后有疑问,今天就结合API单独讲一次. Adapter : An Adapter object acts as ...

随机推荐

  1. 解决“未启用当前数据库的 SQL Server Service Broker,因此查询通知不受支持。如果希望使用通知,请为此数据库启用 Service Broker”错误

    转自:http://blog.csdn.net/andrew_wx/article/details/6947317 解决办法: ALTER DATABASE 数据库名称 SET NEW_BROKER  ...

  2. 【extjs】 ext5 Ext.grid.Panel 分页,搜索

    带有分页,搜索的grid. <%@page language="java" contentType="text/html; charset=UTF-8" ...

  3. CryptAPI 数字签名 与 Openssl 验证签名

    这段时间要实现一个认证协议,分为客户端和服务器端,客户端使用windows操作系统,服务器端使用linux操作系统,在客户端下(windows),使用windows证书库中的签名证书对消息进行签名(使 ...

  4. css3之transform的应用

    一.利用transform实现图片额外显示 效果图如下 初始状态:

  5. 报错:java.io.FileNotFoundException: (系统找不到指定的路径。)

    报错如下: java.io.FileNotFoundException: E:\apache-tomcat-8.0.37\webapps\20161028-FileUpLoad\WEB-INF\fil ...

  6. [系统集成] CI持续集成项目简介

    一.问题的产生 公司的多个部门围绕着产品开发.测试.发布.维护,设置有不同的岗位和系统,这些岗位和系统缺少有效的整合,没有实现自动化,效率不是很高,因此就有了CI(持续集成)的项目需求. 二.解决方案 ...

  7. 4. 对list进行sort

    一. sort命令 sort命令可以对list排序 sort命令把字段转先换为double类型在进行比较 sort排序list 127.0.0.1:6379> lrange list2 0 -1 ...

  8. 3. redis的超时,事务,watch

    一. 键的生存时间 expire设置一个已经存在的key的生存时间,到时间后redis自动删除这个 命令 expire 设置生存时间(单位/秒) pexpire设置生存时间(单位/毫秒) ttl/pt ...

  9. fork()详解

    参照: http://blog.csdn.net/jason314/article/details/5640969 http://coolshell.cn/articles/7965.html

  10. (C/C++) Interview in English - Basic concepts.

    Question Key words Anwser A assignment operator abstract class It is a class that has one or more pu ...