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:

由服务获取:

  1. LayoutInflater inflater = (LayoutInflater)context.getSystemService
  2. (Context.LAYOUT_INFLATER_SERVICE);
  1. LayoutInflater inflater = (LayoutInflater)context.getSystemService
  2. (Context.LAYOUT_INFLATER_SERVICE);
LayoutInflater inflater = (LayoutInflater)context.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);

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

关于inflate的第3个参数

2012-2-19 09:24| 发布者: benben| 查看: 5168| 评论: 0

 

摘要: 方法 inflate(int resource, ViewGroup root, boolean attachToRoot) 中,前连个参数都好理解,我比较费解的是第3个参数。文档中的解释是:Whether the inflated hierarchy should be attached to the root paramete ...

 

 

方法 inflate(int resource, ViewGroup root, boolean attachToRoot) 中,前连个参数都好理解,我比较费解的是第3个参数。

文档中的解释是:Whether the inflated hierarchy should be attached to the root parameter? If false, root is only used to create the correct subclass of LayoutParams for the root view in the XML.
 
举个例子看一下
新建一个工程
 
工程包含两个xml文件
layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
 
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
   
    <FrameLayout
        android:id="@+id/ffff"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">FrameLayout>
 
LinearLayout>
 
layout/ffff.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
 
    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="CheckBox" />
 
LinearLayout>
 
接下来看activity中怎么写的
这里分3中情况
first, no attachToRoot params
activity 中的部分代码,注意看红色部分
        setContentView(R.layout.main);
        ViewGroup v = (ViewGroup) findViewById(R.id.ffff);
        View vv = LayoutInflater.from(this).inflate(R.layout.ffff, v);
 

 
布局结构图

 
Second, params attachToRoot is false
View vv = LayoutInflater.from(this).inflate(R.layout.ffff, v, false);
 

发现没有了ffff.xml 中的内容
通过结构图查看,确实没有了

 
Third,
        ViewGroup v = (ViewGroup) findViewById(R.id.ffff);
        View vv = LayoutInflater.from(this).inflate(R.layout.ffff, v, false);
        v.addView(vv);
 
运行结果

 
呵呵,又有了。
 
所以这个参数的作用就是,是否把选取的视图加入到root中。false 的意思就是不添加到root中。可能需要我们手动添加。

LayoutInflater的更多相关文章

  1. 浅谈 LayoutInflater

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

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

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

  3. Android下LayoutInflater的使用

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

  4. Android 中 LayoutInflater 的使用

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

  5. Android 生成LayoutInflater的三种方式

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

  6. Android LayoutInflater详解

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

  7. [Android] 转-LayoutInflater丢失View的LayoutParams

    原文地址:http://lmbj.net/blog/layoutinflater-and-layoutparams/ View view = inflater.inflate(R.layout.ite ...

  8. LayoutInflater的使用

    在实际工作中,事先写好的布局文件往往不能满足我们的需求,有时会根据情况在代码中自定义控件,这就需要用到LayoutInflater.LayoutInflater在Android中是“扩展”的意思,作用 ...

  9. 6、Android之LayoutInflater.inflate()

    LayoutInflater.inflate()的作用就是将一个xml定义的布局文件实例化为view控件对象: 与findViewById区别: LayoutInflater.inflate是加载一个 ...

  10. Android 查缺补漏之Adapter 和 LayoutInflater

    在之前的博客我也讲过 Adapter 和 inflater,但发现讲的不够清楚,好多读者看后有疑问,今天就结合API单独讲一次. Adapter : An Adapter object acts as ...

随机推荐

  1. maven pom.xml加载不同properties配置

    1.pom.xml =========================== <!-- 不同的打包环境配置: test=开发/测试测试环境,  product=生产环境; 命令行方式: mvn c ...

  2. Oracle Data Guard

    DG 是 Oracle Data Guard 的简称.也就是Oracle11g的 数据卫士. 由于在工作中 Oracle和 SQL SERVER2008 同时都需要维护管理.给我的感觉这里的 DG 其 ...

  3. windows和linux间互传文件

    方法1:Xshell传输文件 用rz,sz命令在xshell传输文件 很好用,然后有时候想在windows和linux上传或下载某个文件,其实有个很简单的方法就是rz,sz 首先你的Ubuntu需要安 ...

  4. Oracle学习网址

    Oracle Error Search: http://www.ora-error.com/ Oracle Database Error Message - Oracle Documentation: ...

  5. (一)NUnit单元测试心得

    由于各种缘由,一本<.Net单元测试艺术>突然出现在了我的办公桌上,于是我的单元测试之路就此开始.通过一两个月不间断的学习,以及不断结合具体的项目做开发,再结合书上的知识对单元测试有了一些 ...

  6. PHP 实现短域名互转

    /** * 短域名生成&解析类 */ class Build_URL { private $mem; private $base_url = 'http://xxx.com/'; public ...

  7. Android UI -- 布局介绍(布局包括FrameLayout, LinearLayout, RelativeLayout, GridLayout)

    首先介绍常用布局类 FrameLayout 最简单的布局管理器. 这个布局管理类有几个特性: 添加组件默认在左上角的. 如果添加多个组件会叠加到一起,并且都在左上角.(可以通过一gravity属性改变 ...

  8. bzoj 2816: [ZJOI2012]网络(splay)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2816 [题意] 给定一个无向图,满足条件:从一个节点出发的同色边不超过2条,且不存在同 ...

  9. NOIP2012 国王游戏

    2国王游戏 (game.cpp/c/pas) [问题描述] 恰逢 H 国国庆,国王邀请 n 位大臣来玩一个有奖游戏.首先,他让每个大臣在左.右手上面分别写下一个整数,国王自己也在左.右手上各写一个整数 ...

  10. 【转载】shell中的特殊变量$

    shell中的特殊变量:变量名含义$0shell或shell脚本的名字$*以一对双引号给出参数列表$@将各个参数分别加双引号返回$#参数的个数$_代表上一个命令的最后一个参数$$代表所在命令的PID$ ...