获得 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. Genymotion出现Unable to load VirtualBox engine问题--100%解决

    首先强调一个事情,作为程序员,一定要养成一个习惯:安装软件时尤其像VirtualBox这类,首先设置其"兼容模式"以及"特权等级" . 环境:win7 64bi ...

  2. ubuntu 创建快捷方式

    sudo ln -s /opt/eclipse/eclipse /usr/bin/eclipse 这样就可以在命令行中敲:eclipse,来打开eclipse了. 应该还有一种方式,就像快捷方式一样. ...

  3. beeframework开发笔记1

    1.商品页弹出规格去掉(null) wzmzy/shop/model/extensions/GOOD_SPEC_VALUE+TagList.m 2.

  4. 在使用Redis的客户端连接工具ServiceStack.Redis要注意的问题

    在使用Redis的客户端连接工具ServiceStack.Redis要注意的问题   Redis是一个非常NB的内存级的数据库,我们可以把很多”热数据“(即读写非常多的数据)放入其中来操作,这样就减少 ...

  5. 【KVM】Ubuntu14.04 安装KVM

    1. 首先检查系统是否支持CPU虚拟化 # egrep -o "svm|vmx" /proc/cpuinfo 若显示如下类似信息,则说明支持CPU虚拟化 vmx vmx ... v ...

  6. 五大Android布局方式浅析

    Android布局是应用界面开发的重要一环,在Android中,共有五种布局方式,分别是:FrameLayout(框架布局),LinearLayout (线性布局),AbsoluteLayout(绝对 ...

  7. 简单实例讲解linux的module模块编译步骤

    注:原博文地址http://blog.sina.com.cn/s/blog_4ba5b45e0102v25h.html ---------------------------------------- ...

  8. setValue:forUndefinedKey

    *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UIViewControlle ...

  9. java数组初始化的三种方式

      //第一种 int[] is= new int[3]; is[0]=1; is[1]=2; is[2]=3; //第二种 int[] is2= {1,2,3}; //第三种 int[] is3= ...

  10. PLSQL_闪回操作6_Flashback Database

    2014-12-09 Created By BaoXinjian