在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById()。不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例化;而findViewById()是找xml布局文件下的具体widget控件(如Button、TextView等)。
具体作用:
1、对于一个没有被载入或者想要动态载入的界面,都需要使用LayoutInflater.inflate()来载入;

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

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

public abstract class LayoutInflater extends Object

获得 LayoutInflater 实例的三种方式

LayoutInflater inflater = getLayoutInflater();  //调用Activity的getLayoutInflater()

LayoutInflater localinflater =  (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

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 new AssertionError("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 (int resource, 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)findViewById(R.id.content);// error EditText editText = (EditText)view.findViewById(R.id.content);

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

注意:

·inflate 方法与 findViewById 方法不同;

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

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

转自 http://blog.sina.com.cn/s/blog_48a45b950100u8ax.html

LayoutInflater的获取与使用的更多相关文章

  1. LayoutInflater的获取方式

    1.LayoutInflater是什么? 通俗而讲,就是将xml中定义的布局找出来. 2.获取LayoutInflater的三种方式   1. LayoutInflater inflater = ge ...

  2. layoutInflater的用途以及获取VIEW方法

    如果需要用到自定义多个布局,就需要用到layoutInflater,获取layoutInflater一般有几种方式,但我在实际使用中,感觉如下的getLayoutInflater()是最为方便的,不用 ...

  3. Android开发之自定义的ListView(UITableViewController)

    Android开发中的ListView, 顾名方法思义,就是表视图.表示图在iOS开发中就是TableView.两者虽然名称不一样,但是其使用方法,使用场景以及该控件的功能都极为相似,都是用来展示大量 ...

  4. 高灵活低耦合Adapter快速开发攻略

    Android开发中经常需要使用Adapter. 传统方法是自定义一个Adapter并继承AndroidSDK内的BaseAdapter, 这种方式代码量大,耦合度高,灵活性差(各种监听事件需要对Vi ...

  5. 获取LayoutInflater对象的方法和inflate方法的一些参数问题

    一.获取LayoutInflater的三种方法 1. LayoutInflater layoutInflater = (LayoutInflater) MainActivity.this.getSys ...

  6. 获取LayoutInflater的三种方式

    1.LayoutInflater.from(mContext); 2.Activity类的getLayoutInflater 3.(LayoutInflater)mContext.getSystemS ...

  7. 浅谈 LayoutInflater

    浅谈 LayoutInflater 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 源码:AndroidDemo/View 文中如有纰漏,欢迎大家留言指出. 在 Android 的 ...

  8. Android下LayoutInflater的使用

    在我们想XML布局文件转换为View对象的时候.我们都会使用LayoutInflate对象.顾名思义咋一眼就能看出来他是布局填充器.那么接下来看看LayoutInfalte的使用 总体分为 Layou ...

  9. Android 中 LayoutInflater 的使用

    一.LayoutInflater 的作用 我们一般使用 LayoutInflater 做一件事:View inflate(int resource, ViewGroup root); inflate( ...

随机推荐

  1. Android+OpenCV 摄像头实时识别模板图像并跟踪

    通过电脑摄像头识别事先指定的模板图像,实时跟踪模板图像的移动[用灰色矩形框标识] ps:一开始以为必须使用OpenCV Manager,可是这样会导致还需要用户去额外安装一个apk,造成用户体验很差, ...

  2. [Javascript] Gradient Fills on the HTML5 Canvas

    window.onload = function() { var canvas = document.getElementById("canvas"), context = can ...

  3. 自定义QT事件

    qtuserclass 1.事件ID 本质上就是一个整型值.0-999作为系统保留字段,自定义事件从QEvent::User(也就是1000)开始, 例如: #define MY_EVENT QEve ...

  4. jsp----在jsp中写java代码(变量和函数方法)

    <%@page import="java.text.SimpleDateFormat"%><%@page language="java" im ...

  5. 20 个强大的 Sublime Text 插件

    20. FTPSync 默认情况下SublimeText不具备FTP的功能,如果你正在寻找能在您的SublimeText应用程序中使用的免费和易用的FTP工具,你可以考虑FTPSync.这是一个非常简 ...

  6. [Form Builder]POST 与 commit_form 的区别

    commit_form:将form上的数据写入数据库,并且会在database提交,即 直接查询表是能够查到结果,在form左下角会得到“FRM-40400:Transaction complete: ...

  7. 远程之SSH

    SSH(Secure Shell的缩写),由 IETF 的网络工作小组(Network Working Group)所制定:SSH 为建立在应用层和传输层基础上的安全协议.SSH 是目前较可靠,专为远 ...

  8. 解决Ubuntu下sublime中不能输入中文的问题

    解决Ubuntu下sublime中不能输入中文的问题 Ubuntu下安装sublime后,不能输入中文,而在其他软件中能正常输入,这是sublime的bug,解决方案是在通过shell在每次运行sub ...

  9. C++学习(四)

    一.拷贝构造函数和拷贝赋值运算符1.拷贝构造:用一个已有的对象,构造和它同类型的副本对象——克隆.2.形如class X {  X (const X& that) { ... }};的构造函数 ...

  10. Linux操作系统搭建JDK开发环境

    在Linuxer系统中,jdk环境常常是必需的,下面介绍下Oracle官方JDK安装方式 第一步:去jdk官网下载对应版本的JDK安装文件 第二步:解压文件,修改文件名 $ sudo mkdir /u ...