【转自】http://blog.csdn.net/scut1135/article/details/7055461

通俗的说,inflate就相当于将一个xml中定义的布局找出来.
因为在一个Activity里如果直接用findViewById()的话,对应的是setConentView()的那个layout里的组件.

因此如果你的Activity里如果用到别的layout,比如对话框上的layout,你还要设置对话框上的layout里的组件(像图片ImageView,文字TextView)上的内容,你就必须用inflate()先将对话框上的layout找出来,然后再用这个layout对象去找到它上面的组件,如:
View view = View.inflate(this, R.layout.dialog_layout, null);
TextView dialogTV = (TextView) view.findViewById(R.id.dialog_tv);
dialogTV.setText("abcd");

如果组件R.id.dialog_tv是对话框上的组件,而你直接用this.findViewById(R.id.dialog_tv)肯定会报错.

三种方式可以生成LayoutInflater :
LayoutInflater inflater = LayoutInflater.from(this);
LayoutInflater inflater = getLayoutInflater();
LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
然后调用inflate方法将xml布局文件转成View
public View inflate (int resource, ViewGroup root, boolean attachToRoot)

在View类中,也有inflate方法
public static View inflate (Context context, int resource, ViewGroup root)

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Android - LayoutInflate用法

2011-09-15 11:21

在实际开发中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()方法,看一下该源代码:

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 等)。

转载于:https://www.cnblogs.com/DonkeyTomy/articles/2595606.html

【转】 Android - LayoutInflate用法相关推荐

  1. MTK驱动(77)---Android getevent用法

    Android getevent用法 getevent命令用法如下: Usage: getevent [-t] [-n] [-s switchmask] [-S] [-v [mask]] [-d] [ ...

  2. Android inflater用法

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

  3. android plurals用法

    参考 android plurals用法 对一个给定的语言和数字来说,决定使用哪一个case的规则是很复杂的,所以android提供了方法getQuantityString(),它可以用来为你选择合适 ...

  4. android plurals用法(单复数)

    0.相关文章 android plurals用法 Android中的string资源占位符及Plurals string 1.使用 对一个给定的语言和数字来说,决定使用哪一个case的规则是很复杂的, ...

  5. Android.mk 用法介绍

    一个Android.mk file用来向编译系统描述你的源代码.具体来说:该文件是GNU Makefile的一小部分,会被编译系统解析一次或多次.你可以在每一个Android.mk file中定义一个 ...

  6. android五大布局的用法,android:stretchColumns用法

    TableLayout是一个使用复杂的布局,最简单的用法就仅仅是拖拉控件做出个界面,但实际上,会经常在代码里使用TableLayout,例如做出表格的效果.本文主要介绍TableLayout的基本使用 ...

  7. Android Scroller用法

    为什么80%的码农都做不了架构师?>>>    概述 Android里Scroller类是为了实现View平滑滚动的一个Helper类.通常在自定义的View时使用,在View中定义 ...

  8. android queue用法,GitHub - rygz146/TQueue: Android 可以任意切换线程的任务队列, TQueue

    TQueue 一个Android开发库, 可以任意切换线程的链式调用任务队列, 可添加定时, 延时任务, 统一异常处理(Ceased中断),但不影响整个任务链的运行. 具体用法如下: TQueue.q ...

  9. android组件用法说明,Android第三方控件PhotoView使用方法详解

    Android第三方控件PhotoView使用方法详解 发布时间:2020-10-21 15:06:09 来源:脚本之家 阅读:74 作者:zhaihaohao1 PhotoView的简介: 这是一个 ...

最新文章

  1. git remote 命令
  2. VS Code 调试Node.js express网站
  3. Android ImageView图片显示点击背景切换
  4. 华为路由器RIP协议通信的配置
  5. Kotlin 继续助力 Android 开发,并计划涉足更多领域
  6. 如何转移域名到GoDaddy.com
  7. vc6开发一个抓包软件_开发一个软件多少钱?3种软件开发公司报价
  8. vue04-components
  9. 如何用shell脚本读取配置文件
  10. vue 圆形 水波_canvas 水滴图、液体进度条、仿加速球、圆球水波图
  11. LFW数据集—人脸对齐
  12. Javaweb-学习路线
  13. 人民币大写金额转换C#方法
  14. php 七牛视频鉴黄,API调用视频鉴黄v1(不推荐)
  15. 康托尔、哥德尔、图灵——永恒的金色对角线(rev#2)
  16. 给小学生科普计算机知识,小学生科普知识演讲稿
  17. cada0图纸框_按1:1画图后如何出A0图纸图框怎么设置?
  18. Mybatis-9.28
  19. 前端-CSS样式的简单介绍
  20. 做人如水 做事如山

热门文章

  1. Windows Phone开发基础(11)实现一个RSS阅读器
  2. JSP FORM 提交
  3. UVA 10245 The Closest Pair Problem
  4. 让你的主机运行ASP.NET 2.0 AJAX程序
  5. 哎..前段时间的偷懒..造成今天的被动局面...要检讨深刻教训.
  6. 浅谈SpringCloud (二) Eureka服务发现组件
  7. 正睿 2018 提高组十连测 Day4 T3 碳
  8. IE浏览器上传文件时本地路径变成”C:\fakepath\”的问题
  9. 路飞学院-Python爬虫实战密训班-第2章
  10. Gitlab环境快速部署(RPM包方式安装)