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

获取它的用法有3种:

方法1:

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

static LayoutInflater from(Context context);

如:

  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);
  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);
LayoutInflater inflater = LayoutInflater.from(this);    

View view=inflater.inflate(R.layout.ID, null);     

//或写成:     

View view=LayoutInflater.from(this).inflate(R.layout.ID, null);

方法2:

由服务获取:

方法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开发中,那些让您觉得相见恨晚的方法、类或接口

    Android开发中,那些让你觉得相见恨晚的方法.类或接口本篇文章内容提取自知乎Android开发中,有哪些让你觉得相见恨晚的方法.类或接口?,其实有一部是JAVA的,但是在android开发中也算常 ...

  2. Android学习探索之Java 8 在Android 开发中的应用

    前言: Java 8推出已经将近2年多了,引入很多革命性变化,加入了函数式编程的特征,使基于行为的编程成为可能,同时减化了各种设计模式的实现方式,是Java有史以来最重要的更新.但是Android上, ...

  3. android开发中fragment获取context

    在用到fragment时无法使用.this来指定当前context内容,android开发中fragment获取context,可以使用getActivity().getApplicationCont ...

  4. java中的反射机制在Android开发中的用处

    JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法和属性:这种动态获取的信息以及动态调用对象的方法的功能称为java语言的反 ...

  5. Android开发中的输入合法性检验

    Why ? 合法性检查对于程序的健壮性具有重要作用.在Android开发中,良好的合法性检查设计机制可以使程序更加清晰,产生bug更少,交互更加友好. What ? 合法性检查的目的在于确定边界.对于 ...

  6. 在android开发中使用multdex的方法-IT蓝豹为你整理

    Android系统在安装应用时,往往需要优化Dex,而由于处理工具DexOpt对id数目的限制,导致其处理的数目不能超过65536个,因此在Android开发中,需要使用到MultiDex来解决这个问 ...

  7. 怎样实现了捕获应用中的日志在android开发中

    怎样实现了捕获应用中的日志在android开发中,大家可研究一下. Process mLogcatProc = null; BufferedReader reader = null; try { mL ...

  8. Android开发中Eclispe相关问题及相应解决(持续更新)

    1.Eclipse项目中的Android Private Libraries没有自动生成. 一般而言,在Android开发中,项目中引用到的jar包会放到项目目录中的libs中,引入库会放到Andro ...

  9. Android开发中的问题及相应解决(持续更新)

    最近博客写的少了,以后还得经常更新才行. ------------------------------------------------------------ 1.特定业务需求下try cath ...

随机推荐

  1. 第8章 Android数据存储与IO——File存储

    openFileOutput/openFileInput 这是android自带的两种解决方案.

  2. 安装AD15有问题多数是因为旧版本AD软件没有卸载干净,清理方法详解

    论坛中总会看到有些朋友安装AD14.x,AD15.x后,使用不正常,多数情况是因为旧版本的AD软件没有卸载干净,安装新版本AD软件后,就会有问题.卸载和清理AD旧版本软件的方法如下(此方法只能解决卸载 ...

  3. Could not find *.apk!解决办法

    右键点击项目选择Properties,把Libraries下Android x.x给remove了. 点右侧的Add Library,选择JRE System Library然后next,重新指定JR ...

  4. JAVA中的时间操作

    java中的时间操作不外乎这四种情况: 1.获取当前时间 2.获取某个时间的某种格式 3.设置时间 4.时间的运算 好,下面就针对这四种情况,一个一个搞定. 一.获取当前时间 有两种方式可以获得,第一 ...

  5. NOI2012 魔幻棋盘

    http://www.lydsy.com/JudgeOnline/problem.php?id=2877 二维线段树. 好恶...... B类数据: 棋盘是一维的. 我们有一个结论: $gcd(a_{ ...

  6. bzoj2154

    #include<cstdio> #include<cstdlib> #include<iostream> #include<fstream> #inc ...

  7. c#秒转时分秒

          2个办法 @{             int hour = item.track / 3600;             int min = (item.track - hour * 3 ...

  8. Interfaces

    阅读Java的官方Doc,总结如下. What is Interface An interface is a reference type, similar to a class, that can ...

  9. hdu 5167 Fibonacci(预处理)

    Problem Description Following is the recursive definition of Fibonacci sequence: Fi=⎧⎩⎨01Fi−1+Fi−2i ...

  10. linux的chattr和lsattr命令

    chattr是用来改变文件.目录属性.chattr命令的作用很大,其中一些功能是由linux内核版本来支持的.通过chattr命令修改属性能够提高系统的安全性,但是它并不适合所有的目录.lsattr是 ...