通俗的说,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)

LayoutInflater.inflate()

其实,这三种方式本质是相同的,从源码中可以看出:

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

inflater-布局转化实现的更多相关文章

  1. 实例化Layout中的布局文件(xml)

    什么是LayoutInflater This class is used to instantiate layout XML file into its corresponding View obje ...

  2. getViewById和getLayoutInflater().inflate的用法

    getViewById和getLayoutInflater().inflate得用法 1.什么是LayoutInflaterThis class is used to instantiate layo ...

  3. 深入理解Android 自定义attr Style styleable以及其应用

    相信每一位从事Android开发的猿都遇到过需要自己去自定义View的需求,如果想通过xml指定一些我们自己需要的参数,就需要自己声明一个styleable,并在里面自己定义一些attr属性,这个过程 ...

  4. 打造android偷懒神器———RecyclerView的万能适配器

    转载请注明出处谢谢:http://www.cnblogs.com/liushilin/p/5720926.html 很不好意思让大家久等了,本来昨天就应该写这个的,无奈公司昨天任务比较紧,所以没能按时 ...

  5. Android异步加载

    一.为什么要使用异步加载? 1.Android是单线程模型 2.耗时操作阻碍UI线程 二.异步加载最常用的两种方式 1.多线程.线程池 2.AsyncTask 三.实现ListView图文混排 3-1 ...

  6. Android成长日记-使用PagerAdapter实现页面切换

    Tip:此方式可以实现页面切换 1. 创建view1.xml,view2.xml,view3.xml,main.xml 在main.xml中创建 <android.support.v4.view ...

  7. Android 开发 之 Fragment 详解

    本文转载于 : http://blog.csdn.net/shulianghan/article/details/38064191 本博客代码地址 : -- 单一 Fragment 示例 : http ...

  8. 关于BaseAdapter的使用及优化心得(一)

    对于Android程序员来说,BaseAdapter肯定不会陌生,灵活而优雅是BaseAdapter最大的特点.开发者可以通过构造BaseAdapter并搭载到ListView或者GridView这类 ...

  9. UML活动图(转载)

    概述: 活动图是另一个重要的UML图来描述系统的动态方面. 活动图基本上是代表流程形成一个活动到另一个活动的流程图.活动可以被描述为一个系统的操作. 因此,绘制控制流从一个操作到另一个.此流可以是连续 ...

  10. Fragment 总结

    本博客代码地址 : -- 单一 Fragment 示例 : https://github.com/han1202012/Octopus-Fragement.git -- 可复用的 Fragment 示 ...

随机推荐

  1. 用eclipse 检索SVN 上 myEclipse 建的web项后,成java项目解决方法

    用eclipse 检索SVN 上 myEclipse 建的web项后,成java项目解决方法 在网上找了非常多,都无论用. 说添加.project 文件几个属性.但我发现里面都有,在我这里无论什么用. ...

  2. HDU 5303 Delicious Apples(贪心 + 背包 2015多校啊)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5303 Problem Description There are n apple trees plan ...

  3. 有关c语言指针的总结

    #include<stdio.h> void main() { int a[3]={1,3,5};//一维数组 int *num[3]={&a[0],&a[1],& ...

  4. modSecurity规则学习(六)——检测模式

    传统检测模式-自主规则 传统检测模式所有规则都是“闭环”的模式.就像HTTP本身一样,单独的规则是无状态的.这意味着规则之间不共享信息,每个规则都没有关于任何先前规则匹配的信息.它仅使用其当前的单个规 ...

  5. android CoordinatorLayout使用

    一.CoordinatorLayout有什么作用 CoordinatorLayout作为“super-powered FrameLayout”基本实现两个功能: 1.作为顶层布局 2.调度协调子布局 ...

  6. legend---三、方法集思路

    legend---三.方法集思路 一.总结 一句话总结:其实也就是工具包思路,会极大的简化编程,清晰逻辑 1.多if转换成简洁单if怎么实现? 下面这段代码是错的,if的这种写法只适合直接return ...

  7. 1.windows(64位)下使用curl命令

    转自:https://www.cnblogs.com/xing901022/p/4652624.html Curl命令可以通过命令行的方式,执行Http请求.在Elasticsearch中有使用的场景 ...

  8. POJ 3193 字符串排序+比较

    思路: 1. 先把那m个排个序 此后每回二分+strncmp一下就好了 strncmp是个好东西啊-- 2. hash判判 (注意 hash会有冲突--------.) //By SiriusRen ...

  9. thinkserer TD350 系统损坏后,数据恢复及系统重做过程

    电脑配置: 联想服务器 TD350   E5-2609V4 2*8G 2*4T+R1 塔式 单电 1.系统恢复: 试过很多种方法,均无效 2.数据恢复:  重新安装系统后,直接在D盘查找 , 原C盘的 ...

  10. Felx之HTTPService

    获取并显示数据 为了向我们的程序提供数据,Adobe Flex包含特别为与HTTP服务器,网络服务或者是远程对象服务(Java对象)进行交互的而设计的组件.这些组件被称之为远程过程调用(RPC)服务组 ...