部分转载自:http://www.cnblogs.com/shitianzeng/articles/2323427.html

在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById()。不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例化;而findViewById()是找xml布局文件下的具体widget控件(如Button、TextView等)。

具体作用:

1、对于一个没有被载入或者想要动态载入的界面,都需要使用LayoutInflater.inflate()来载入;

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

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

public abstractclass 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):

public static LayoutInflater from(Context context) {

LayoutInflater LayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);                        if (LayoutInflater == null) {

throw newAssertionError("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 (intresource, 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)view.findViewById(R.id.content);

对于上面代码,指定了第二个参数 ViewGroup root,当然你也可以设置为 null 值。

注意:

·inflate方法与 findViewById 方法不同;

·inflater 是用来找 res/layout下的 xml 布局文件,并且实例化;

·findViewById() 是找具体 xml 布局文件中的具体 widget 控件(如:Button、TextView 等)。

android Inflater相关推荐

  1. Android inflater用法

    Android inflater用法 简介 具体作用 获取layoutInflater的三种方式 源码查看 返回值结果 注意 简介 在实际的开发中layoutInflater这个类的主要的主要的作用类 ...

  2. android inflater,探索 Android Inflater.inflate()

    android-knowledge.jpg 在 Android 开发中,有很多看起来 "约定俗成" 的代码,有时候不妨停下来想一想深层次的含义.最近我在给 Fragment 填充布 ...

  3. android之inflater介绍

    image.png LayoutInflater(布局加载器) 开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById(). 不同点是LayoutInflate ...

  4. inflater用法

    android inflater 用法 在 实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById().不同点是LayoutInflater是用 来找res ...

  5. android之inflater用法

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

  6. android layout.inflater,Android - LayoutInflater

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

  7. 【Android】inflater的作用

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

  8. Android 悬浮窗口

    Android 悬浮窗口 一.创建悬浮窗口步骤     1.实现一个ViewGroup类,作为悬浮窗口的界面类,以便在里面重写onInterceptTouchEvent和onTouchEvent方法, ...

  9. android xUtils的使用

    gethub地址:https://github.com/wyouflf/xUtils/ xUtils简介 xUtils 包含了很多实用的android工具. xUtils 支持大文件上传,更全面的ht ...

最新文章

  1. 互联网公益平台米多乐获近千万天使轮融资,熊猫资本投资...
  2. 年终凡尔赛,都是别人家的公司...
  3. thinkphp5部署nginx服务上多站点解决方案!
  4. 2020年, VQA论文汇总
  5. 机器学习入门06 - 训练集和测试集 (Training and Test Sets)
  6. prometheus.yml 配置文件参数详解
  7. java定义一个父类circle_[转载]java编程题全集一(答案)
  8. Sql Server 中常用的字符串函数
  9. 网络规划设计师教程pdf版下载
  10. arcgis怎么做poi_基于POI数据与ArcGIS空间分析技术的城市公交站点现状容量评价方法...
  11. 什么是侧翼区(flanking region)和侧翼区单核苷酸多态性(Flanking SNPs)
  12. 计算机页面打不开非常卡,电脑卡的厉害几乎打不开网页怎么办
  13. 4种方法教你如何查看java对象所占内存大小
  14. Android12前台服务问题
  15. 解决电脑上装了两个不同版本的sqlserver 导致索引越界等一系列问题。
  16. phyton基础-01
  17. html怎么动态背景,HTML 动态背景
  18. 正睿OIday1总结
  19. 算法学习--分酒问题(BFS)
  20. 《我为什么熬夜?》系列之 倚天屠龙记

热门文章

  1. 季胺化聚苯乙烯微球载纳米铁/镍降解氯代硝基苯/载金纳米粒子聚苯乙烯/聚丙烯酸微球的探究
  2. 国美金融贷款BootLoader程序,国美金融贷款启动操作系统内核
  3. mapbox热力图属性
  4. 哈夫曼abcdef编码
  5. IDEA全局搜索(Find in Path)排除log文件/不搜索指定文件夹
  6. 【区块链技术工坊33期】罗梅琴:区块链账户模型
  7. 小米 红米Note(联通3G版)线刷兼救砖_解账户锁_纯净刷机包_教程
  8. 金三银四面试“超全秘籍”
  9. android插件式开发(如QQ表情、微信表情等)
  10. RS232\RS485\TTL接口\UART