LayoutInflater的实例化
获得 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的实例化的更多相关文章
- LayoutInflater和inflate()方法的用法
LayoutInflater作用是将layout的xml布局文件实例化为View类对象. 实现LayoutInflater的实例化共有3种方法, (1).通过SystemService获得 Layou ...
- Android LayoutInflater&LayoutInflaterCompat源码解析
本文分析版本: Android API 23,v4基于 23.2.1 1 简介 实例化布局的XML文件成相应的View对象.它不能被直接使用,应该使用getLayoutInflater()或getSy ...
- LayoutInflater和inflate的用法,有图有真相
1.概述 有时候在我们的Activity中用到别的layout,并且要对其组件进行操作,比如: A.acyivity是获取网络数据的,对应布局文件为A.xml,然后需要把这个数据设置到B.xml的组件 ...
- Android群英传笔记——第四章:ListView使用技巧
Android群英传笔记--第四章:ListView使用技巧 最近也是比较迷茫,但是有一点点还是要坚持的,就是学习了,最近离职了,今天也是继续温习第四章ListView,也拖了其实也挺久的了,list ...
- LayoutInflater这个类还是非常有用的,它的作用类似于findViewById()。 但他是找XML文件并实例化
在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById().不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例 ...
- Android 中 LayoutInflater 的使用
一.LayoutInflater 的作用 我们一般使用 LayoutInflater 做一件事:View inflate(int resource, ViewGroup root); inflate( ...
- Android LayoutInflater详解
在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById().不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且 ...
- LayoutInflater的使用
在实际工作中,事先写好的布局文件往往不能满足我们的需求,有时会根据情况在代码中自定义控件,这就需要用到LayoutInflater.LayoutInflater在Android中是“扩展”的意思,作用 ...
- Android 查缺补漏之Adapter 和 LayoutInflater
在之前的博客我也讲过 Adapter 和 inflater,但发现讲的不够清楚,好多读者看后有疑问,今天就结合API单独讲一次. Adapter : An Adapter object acts as ...
随机推荐
- 创建对象的最好方式&最好的继承机制(代码实例)
/* 创建对象的最好方式:混合的构造函数/原型方式, *用构造函数定义对象的所有非函数属性,用原型方式定义对象的函数属性(方法) */ function People(sname){ this.nam ...
- (转)WEB第三方打印控件[ASP.NET常用工具]
本文转载自:http://blog.csdn.net/chz_cslg/article/details/25415347 在B/S模式开发中,打印是个很大的困扰.无论是采用页面直接输出或者引用WORD ...
- beego里面自定义配置文件
beego编译好的exe通过全路径调用会crash,看了半天,发现是解析不到配置文件,研究了下 发现beego自定义配置文件以后,需要手工parse,我表示,以为是自动化的,没想到是半自动化的…… 追 ...
- HDU3415
题目大意: 给出一个有N个数字(-1000..1000,N<=10^5)的环状序列,找出一个长度不大于k的连续子序列,使其和最大. 分析: 我们可以将环状序列从某处切开,变成一行,然后复制前n- ...
- 127 Word Ladder
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...
- 【Linux】系统 之 RAID
本人从事DBA相关的工作,最近遇到了IO抖动伴随shread running抖动的情况,主机宕机重启后备库及下游解析binlog出现损坏的案例,向一些有经验的同事咨询学习,其中最大的嫌疑是:raid卡 ...
- jQurey基础简介
随着WEB2.0及ajax思想在互联网上的快速发展传播,陆续出现了一些优秀的Js框架,其中比较著名的有Prototype.YUI.jQuery. mootools.Bindows以及国内的JSVM框架 ...
- 黄聪:WordPress图片插件:Auto Highslide修改版(转)
一直以来很多人都很喜欢我博客使用的图片插件,因为我用的跟原版是有些不同的,效果比原版的要好,他有白色遮罩层,可以直观的知道上下翻图片和幻灯片放映模式.很多人使用原版之后发现我用的更加帅一些,于是很多人 ...
- 开发高峰时的CPU使用率
- [实变函数]3.2 可测集 (measurable set)
1 $\bbR^n$ 中集合 $E$ 称为可测的 (measurable), 如果 $$\bee\label{3.2:Caratheodory} m^*T=m^*(T\cap E)+m^*(T\cap ...