LayoutInflater作用是将layout的xml布局文件实例化为View类对象。

获取LayoutInflater的方法有如下三种:

LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.main, null);
 
LayoutInflater inflater = LayoutInflater.from(context); (该方法实质就是第一种方法,可参考源代码)
View layout = inflater.inflate(R.layout.main, null);
 
LayoutInflater inflater = getLayoutInflater();(在Activity中可以使用,实际上是View子类下window的一个函数)
View layout = inflater.inflate(R.layout.main, null);

一直有点纠结setContentView和inflate的区别找了一些资料。写了个小程序看了下:

public class MyInflate extends Activity{
    private TextView tv;
    public void OnCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.main);
        //tv = (TextView) findViewById(R.id.tv);
 
        LayoutInflater inflate = LayoutInflater.from(this);
        View view = inflate.inflate(R.layout.main,null);
        setContentView(view);
    }
}

上述注释掉的代码和没有注释掉的代码两种情况是相同的。

区别:
setContentView()一旦调用, layout就会立刻显示UI;而inflate只会把Layout形成一个以view类实现成的对象,有需要时再用setContentView(view)显示出来。一般在activity中通过setContentView()将界面显示出来,但是如果在非activity中如何对控件布局设置操作了,这就需要LayoutInflater动态加载。

public View inflate(int Resourece,ViewGroup root)
作用:填充一个新的视图层次结构从指定的XML资源文件中
reSource:View的layout的ID
root: 生成的层次结构的根视图
return 填充的层次结构的根视图。如果参数root提供了,那么root就是根视图;否则填充的XML文件的根就是根视图。

其余几个重载的inflate函数类似。

[转]LayoutInflater的inflate函数用法的更多相关文章

  1. 转载 LayoutInflater的inflate函数用法详解

    http://www.open-open.com/lib/view/open1328837587484.html LayoutInflater的inflate函数用法详解 LayoutInflater ...

  2. 转载《 LayoutInflater 的inflate函数用法详解》

    很多人在网上问LayoutInflater类的用法,以及inflate()方法参数的含义,现解释如下: inflate()的作用就是将一个用xml定义的布局文件查找出来,注意与findViewById ...

  3. LayoutInflater的inflate函数用法详解

    LayoutInflater作用是将layout的xml布局文件实例化为View类对象. 获取LayoutInflater的方法有如下三种: LayoutInflater inflater=(Layo ...

  4. LayoutInflater的inflate函数用法

    LayoutInflater作用是将layout的xml布局文件实例化为View类对象. 获取LayoutInflater的方法有如下三种: LayoutInflater inflater=(Layo ...

  5. LayoutInflater和inflate的用法,有图有真相

    1.概述 有时候在我们的Activity中用到别的layout,并且要对其组件进行操作,比如: A.acyivity是获取网络数据的,对应布局文件为A.xml,然后需要把这个数据设置到B.xml的组件 ...

  6. Android成长之路-LayoutInflater和inflate的用法

    在这里用Tabhost的例子来说明: package cn.csdn.activity; import android.app.TabActivity; import android.os.Bundl ...

  7. LayoutInflater和inflate()方法的用法

    LayoutInflater作用是将layout的xml布局文件实例化为View类对象. 实现LayoutInflater的实例化共有3种方法, (1).通过SystemService获得 Layou ...

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

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

  9. LayoutInflater和inflate()方法的使用方法

    public static LayoutInflaterfrom(Context context) { LayoutInflaterLayoutInflater = (LayoutInflater)c ...

随机推荐

  1. 转 gl_VertexID的含义

    #version //layout(location = 0) in vec4 VERTEX; uniform mat4 MODEL_MATRIX; uniform mat4 VIEW_MATRIX; ...

  2. ActiveMQ in Action(5) - Clustering

    关键字: activemq 2.5 Clustering    ActiveMQ从多种不同的方面提供了集群的支持.2.5.1 Queue consumer clusters    ActiveMQ支持 ...

  3. MVC3+EF4.1学习系列(三)-----排序 刷选 以及分页

    上篇文章 已经做出了基本的增删改查    但这远远不足以应付实际的项目  今天讲下实际项目中 肯定会有的 排序 刷选  以及分页. 重点想多写点分页的 毕竟这个是任何时候都要有的 而且 我会尽量把这个 ...

  4. MVC中发生System.Data.Entity.Validation.DbEntityValidationException验证异常的解决方法

    发生System.Data.Entity.Validation.DbEntityValidationException这个异常的时候,如果没有用特定的异常类去捕捉,是看不到具体信息的. 通常都是用Sy ...

  5. Linode和DigitalOcean lnmp一键安装包哪个好?

    Linode和DigitalOcean都是非常棒的VPS厂商,512MB内存的VPS每月低到5美元,搭建wordpress网站,非常方便,甚至可以多人共用,服务器足够强悍,跑几个wordpress博客 ...

  6. idea 切换新项目 新窗口 原窗口

    选择 file--setting. 在新打开的窗口中选择 Appearance & Behavior--System Settings 点 Confirm window to open pro ...

  7. 重拾Ruby—新的征程

    作家格拉德威尔在<异类>一书中指出: “人们眼中的天才之所以卓越非凡,并非天资超人一等,而是付出了持续不断的努力.1万小时的锤炼是任何人从平凡变成超凡的必要条件.“ 他将此称为“一万小时定 ...

  8. ejabberd聊天室离线消息

    首先,xmpp服务器是基于ejabberd.离线消息模块是mod_interact,原地址地址:https://github.com /adamvduke/mod_interact: 修改后实现群聊离 ...

  9. iOS解析后台返回的二进制图片

    UIEdgeInsets insets = {0,0,0,0}; [self.showidentifyButton setImageEdgeInsets:insets]; NSData *imageD ...

  10. xtrabackup 2.0.8备份mysql5.1.65报错

    sh : xtrabackup not found innobackupex: fatal error: no 'mysqld' group in MySQL options fix: add inn ...