Inflater英文意思是膨胀,在Android中应该是扩展的意思吧。
LayoutInflater 的作用类似于 findViewById(),不同点是LayoutInflater是用来找layout文件夹下的xml布局文件,并且实例化!而 findViewById()是找具体某一个xml下的具体 widget控件(如:Button,TextView等)。

获取它的用法有3种:

方法1:

由LayoutInflater的静态函数:from(Context context) 获取:

static LayoutInflater from(Context context);

如:

Java代码
  1. LayoutInflater inflater = LayoutInflater.from(this);
  2. View view=inflater.inflate(R.layout.ID, null);
  3. //或写成:
  4. View view=LayoutInflater.from(this).inflate(R.layout.ID, null);
Java代码  
  1. LayoutInflater inflater = LayoutInflater.from(this);
  2. View view=inflater.inflate(R.layout.ID, null);
  3. //或写成:
  4. View view=LayoutInflater.from(this).inflate(R.layout.ID, null);

方法2:

由服务获取:

Java代码
  1. LayoutInflater inflater = (LayoutInflater)context.getSystemService
  2. (Context.LAYOUT_INFLATER_SERVICE);
Java代码  
  1. LayoutInflater inflater = (LayoutInflater)context.getSystemService
  2. (Context.LAYOUT_INFLATER_SERVICE);

方法3:

调用Activity的getLayoutInflater() 函数获取LayoutInflater 对象。

setContentView和inflate区别

转:http://blog.163.com/promise_wg/blog/static/18912001420116241062211/

一般用LayoutInflater做一件事:inflate

inflate这个方法总共有四种形式(见下面),目的都是把xml表述的layout转化为View对象。
其中有一个比较常用,View inflate(int resource, ViewGroup root),另三个,其实目的和这个差不多。
int resource,也就是resource/layout文件在R文件中对应的ID,这个必须指定。
而ViewGroup root则可以是null,null时就只创建一个resource对应的View,不是null时,会将创建的view自动加为root的child。

setContentView()一旦调用, layout就会立刻显示UI;而inflate只会把Layout形成一个以view类实现成的对象,有需要时再用setContentView(view)显示出来
一般在activity中通过setContentView()将界面显示出来,但是如果在非activity中如何对控件布局设置操作了,这需LayoutInflater动态加载
< TextView
android:id="@+id/tview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="ATAAW.COM"
/>
< Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/button"
android:text="按钮"
/>
在程序中动态加载以上布局。
LayoutInflater flater = LayoutInflater.from(this);
View view = flater.inflate(R.layout.example, null);
获取布局中的控件。
button = (Button) view.findViewById(R.id.button);//这里的view为上面获取的view对象
textView = (TextView)view.findViewById(R.id.tview);

LayoutInflater.inflate() 将Layout文件转换为View,专门供Layout使用的Inflater。虽然Layout也是View的子类,但在android中如果想将 xml中的Layout转换为View放入.java代码中操作,只能通过Inflater,而不能通过findViewById()。

findViewById有两种形式
R.layout.xx 是引用res/layout/xx.xml的布局文件(inflate 方法),R.id.xx是引用布局文件里面的组件,组件的id是xx(findViewById方法)。所有的组件id都能用R.id.xx来查看,但是 组件不在setContentView()里面的layout中就无法使用,Activity.findViewById()会出现空指针异常
a. activity中的findViewById(int id)
b. View 中的findViewById(int id)
不同点是LayoutInflater是用来找layout下xml布局文件,并且实例化!而findViewById()是找具体xml下的具体 widget控件(如:Button,TextView等)。

Android中LayoutInflater的使用相关推荐

  1. Android 中LayoutInflater(布局加载器)之介绍篇

    本文出自博客Vander丶CSDN博客,如需转载请标明出处,尊重原创谢谢 博客地址:http://blog.csdn.net/l540675759/article/details/78099358 前 ...

  2. android 中LayoutInflater 的使用

    Inflater 英文意思是膨胀,在Android 中应该是扩展的意思吧. LayoutInflater的作用类似于findViewById(),不同点是LayoutInflater是用来找layou ...

  3. Android中LayoutInflater()方法

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

  4. Android中measure过程、WRAP_CONTENT详解以及 xml布局文件解析流程浅析

    转自:http://www.uml.org.cn/mobiledev/201211221.asp 今天,我着重讲解下如下三个内容: measure过程 WRAP_CONTENT.MATCH_PAREN ...

  5. Android中measure过程、WRAP_CONTENT详解以及xml布局文件解析流程浅析(上)

    在之前一篇博文中<< Android中View绘制流程以及invalidate()等相关方法分析>> ,简单的阐述 了 Android View 绘制流程的三个步骤,即: 1. ...

  6. 【移动开发】Android中强大的适配功能----Fragment(碎片)总结

    作为大多数刚接触Android应用开发的人来说,在一个强大的Activity类中,就可以完成丰富多彩的UI工作,但是杂乱的屏幕分辨率,使得本来好不容易写好的UI,变得不堪入目...该怎么办那? 查阅了 ...

  7. android中设置ListView的选中的Item的背景颜色(附源码)

    http://longyi-java.iteye.com/blog/976067 ListView中没有默认的选择颜色,只有选择Item后的焦点颜色,鼠标点击时Item有颜色,放开鼠标后颜色也就没有了 ...

  8. Android中的资源访问

    Android中的资源是指非代码部分,指外部文件. assets中保存的一般是原生的文件,例如MP3文件,Android程序不能直接访问,必须通过AssetManager类以二进制流的形式来读取. r ...

  9. Android中Fragment+ViewPager的配合使用

    原本在上一篇博客中要讲解一个Fragment的综合应用,但是中间又想到了滑屏方式,所以就分类在总结了一下,(http://smallwoniu.blog.51cto.com/3911954/13089 ...

最新文章

  1. ASP.NET Core Web Razor Pages系列教程一:使用ASP.NET Core 创建一个Razor Pages网络应用程序
  2. 静态库调用中“unrecognized selector sent to instance”错误
  3. Python学习笔记(2)-Python执行方式、变量
  4. 一个数字可以在不损失精度的情况下达到的JavaScript的最高整数值是多少?
  5. 【理论】红黑树的实现原理
  6. CIPAddressCtrl的用法
  7. 奥运信息安全谁说了算?
  8. 语言模型(N-Gram)
  9. 软考 中级职称哪些最热门_我如何利用有史以来最热门的中级故事来建立排行榜。 以及它几乎是怎么死的。...
  10. 学习 python logging(1): 基本用法
  11. shell监控磁盘使用情况
  12. C++小白课本练习4
  13. Borderline-SMOTE算法介绍及Python实现【内附源代码】
  14. 福建省小学四年级上册计算机知识点总结,小学四年级上册数学知识点大全【1-6单元】...
  15. MySQL必知必会总结
  16. 扎克伯格----转自Jessica巨人
  17. Java 进程启停及诊断 Jarboot大改版、焕然一新
  18. VS2017 -error LNK1104: 无法打开文件“msvcprtd.lib”
  19. 9招教你防止电脑辐射
  20. JAVA调用有道API接口对数据库中的中文语句进行翻译

热门文章

  1. matlab 报错 javax,[求助]安装报错,求大佬帮忙
  2. 时间同步软件 windows_电脑便签设置事件时间提醒软件哪个好用
  3. nginx配置文件及工作原理详解
  4. Spring Boot Bean的使用,@Repository,@Service,@Controller,@Component
  5. c3074 无法使用带圆括号的_小学生常见易考标点符号使用方法及练习(含答案)...
  6. Docker(九):Docker实战 安装 Ubuntu
  7. 软件测试员,你该如何快速提高自己的测试技术?
  8. Python游戏开发,Pygame模块,Python从零开始带大家实现魔塔小游戏
  9. mysql是如何管理数据结构_MySQL索引背后的数据结构和原理
  10. python做一个考试系统_请用 Python 语言编写一个简易的系统登录程序。