在实际开发中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 实例的三种方式

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) {
  2. super(context);
  3. mLayoutInflater = LayoutInflater.from(context);
  4. }

可以看出它其实是调用 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 对象,如下:

 
  1. public View inflate (int resource, ViewGroup root)
  2. public View inflate (XmlPullParser parser, ViewGroup root)
  3. public View inflate (XmlPullParser parser, ViewGroup root, boolean attachToRoot)
  4. public View inflate (int resource, ViewGroup root, boolean attachToRoot)

示意代码:

 
  1. LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
  2. View view = inflater.inflate(R.layout.custom, (ViewGroup)findViewById(R.id.test));
  3. //EditText editText = (EditText)findViewById(R.id.content);// error
  4. EditText editText = (EditText)view.findViewById(R.id.content);

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

注意:

·inflate 方法与 findViewById 方法不同;

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

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

Android - LayoutInflater的更多相关文章

  1. Android LayoutInflater布局填充器

    Android LayoutInflater布局填充器 把一份xml布局文件转为View对象,这就是layoutinflater的作用. 对于一个没有被载入或者想要动态载入的界面,都需要使用Layou ...

  2. [旧][Android] LayoutInflater 工作流程

    备注 原发表于2016.06.20,资料已过时,仅作备份,谨慎参考 前言 感觉很长时间没写文章了,这个星期因为回家和处理项目问题,还是花了很多时间的.虽然知道很多东西如果只是看一下用一次,很快就会遗忘 ...

  3. Android LayoutInflater.inflate(int resource, ViewGroup root, boolean attachToRoot)的参数理解

    方法inflate(int resource, ViewGroup root, boolean attachToRoot) 中 第一个参数传入布局的资源ID,生成fragment视图,第二个参数是视图 ...

  4. Android LayoutInflater详解

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

  5. 转载《Android LayoutInflater详解》

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

  6. Android LayoutInflater原理分析,带你一步步深入了解View(一)

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/12921889 有不少朋友跟我反应,都希望我可以写一篇关于View的文章,讲一讲Vi ...

  7. Android LayoutInflater详解(转)

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

  8. Android LayoutInflater详解 (转)

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

  9. Android LayoutInflater原理分析

    相信接触Android久一点的朋友对于LayoutInflater一定不会陌生,都会知道它主要是用于加载布局的.而刚接触Android的朋友可能对LayoutInflater不怎么熟悉,因为加载布局的 ...

  10. Android LayoutInflater 动态地添加删除View

    我想实现点击一个按钮(或其他的事件)添加或删除View,网上找到了LayoutInflater这个类. 下面是我自己一些经验: android官网上LayoutInflater的API:http:// ...

随机推荐

  1. Qt中如何写一个model

    在qt中,用到最多就是model/view的结构来表示数据层及表示层的关系.model用于给view提供数据.那如何来实现一个简单的树形model呢. 实现一个自己的model需要重载以下的方法: Q ...

  2. iOS 使用COPY声明NSSTRING属性

    使用COPY声明NSSTRING属性 2014/05/29 JACE 发表回复 声明一个NSString属性使用copy要优于使用strong.这同样适用于遵守NSCoding协议的不可变类(immu ...

  3. 第三章:推荐系统冷启动与CB

    3.1冷启动问题简介: 冷启动问题(cold start)主要分三类: •     用户冷启动 •     物品冷启动 •     系统冷启动 参考解决方案: •     推热门 •     利用用户 ...

  4. Android权限安全(3)权限的分级和自定义权限

    Android的不同权限分级 Normal 一般apk都可以用, Dangerous 一般apk都可以用,但有提示 SignatureOrSystem 特定的private key签名的或系统的apk ...

  5. Java IO 遇到的错误

    1.java.io.FileNotFoundException: /storage/emulated/0/xxx.txt: open failed: EISDIR (Is a directory) 该 ...

  6. sh.exe": grunt: command not found

    今天在git命令行工具中使用 grunt的时候,总是提示我找不到grunt命令,如: sh.exe": grunt: command not found 但是我运行 npm install ...

  7. poshytip两个实用示例

    <html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> < ...

  8. android gallery 自定义边框+幻灯片

    最近在项目中用到图片轮播,试了Gallery,ViewFlipper,ViewPager,感觉Gallery最符合需求,但是Gallery的系统边框很难看,项目中要求用自己的背景图片. 下面来看一下使 ...

  9. codevs 1135 选择客栈

    这题没什么话说. #include<iostream> #include<cstdio> #include<cstring> #include<algorit ...

  10. dagli最早干了这样一件事儿 Localization of Cardiac-Induced Signal Change in fMRI

    Localization of Cardiac-Induced Signal Change in fMRI 这篇文章是最早做生理噪声相关组织的定位的. 很奇特,因为,这位学者甚至得出了,血管心动等变化 ...