转  http://yxwang0615.iteye.com/blog/1711147

一个Activity里如果直接用findViewById(),对应的是setConentView()的那个layout里的组件,因此如果你的

Activity里如果用到别的layout,你就必须用inflate()先将对话框上的layout找出来,然后再用这个layout对

象去找到它上面的组件,

  1. public View inflate(Context context, int Resourece,ViewGroup root)
  2. 作用:填充一个新的视图层次结构从指定的XML资源文件中
  3. context : The Context object for your activity or application
  4. reSource:View的layout的ID
  5. root: 生成的层次结构的根视图
  6. return 填充的层次结构的根视图。如果参数root提供了,那么root就是根视图;否则填充的XML文件的根就是根视图。
public View inflate(Context context, int Resourece,ViewGroup root)
作用:填充一个新的视图层次结构从指定的XML资源文件中
context : The Context object for your activity or application
reSource:View的layout的ID
root: 生成的层次结构的根视图
return 填充的层次结构的根视图。如果参数root提供了,那么root就是根视图;否则填充的XML文件的根就是根视图。

View view = View.inflate(this, R.layout.dialog_layout, null);

TextView dialogTV = (TextView) view.findViewById(R.id.dialog_tv);

dialogTV.setText("abcd");

setContentView()一旦调用, layout就会立刻显示UI;而inflate只会把Layout形成一个以view类实现成的对象,

有需要时再用setContentView(view)显示出来。一般在activity中通过setContentView()将界面显示出来,

然后才能使用findViewById()是找xml布局文件下的具体widget控件(如Button、TextView等)

但是如果在非activity中如何对控件布局设置操作了,这就需要LayoutInflater动态加载。

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

  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);

或者:

  1. LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
  2. View view=inflater.inflate(R.layout.ID, null);
LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
View view=inflater.inflate(R.layout.ID, null);

android.view.View类中也有一个inflater方法:

  1. public static View inflate (Context context, int resource, ViewGroup root)
  2. Inflate a view from an XML resource. This convenience method wraps the LayoutInflater class, which provides a full range of options for view inflation.
  3. Parameters
  4. context The Context object for your activity or application.
  5. resource The resource ID to inflate
  6. root A view group that will be the parent. Used to properly inflate the layout_* parameters.
public static View inflate (Context context, int resource, ViewGroup root)

Inflate a view from an XML resource. This convenience method wraps the LayoutInflater class, which provides a full range of options for view inflation.
Parameters context The Context object for your activity or application.
resource The resource ID to inflate
root A view group that will be the parent. Used to properly inflate the layout_* parameters.

该方法和LayoutInflater中的inflater方法区别不明,查api也没看明白:

  1. public View inflate (int resource, ViewGroup root)
  2. Inflate a new view hierarchy from the specified xml resource. Throws InflateException if there is an error.
  3. Parameters
  4. resource ID for an XML layout resource to load (e.g., R.layout.main_page)
  5. root Optional view to be the parent of the generated hierarchy.
  6. Returns
  7. The root View of the inflated hierarchy. If root was supplied, this is the root View; otherwise it is the root of the inflated XML file.
public View inflate (int resource, ViewGroup root)
Inflate a new view hierarchy from the specified xml resource. Throws InflateException if there is an error. Parameters resource ID for an XML layout resource to load (e.g., R.layout.main_page)
root Optional view to be the parent of the generated hierarchy. Returns
The root View of the inflated hierarchy. If root was supplied, this is the root View; otherwise it is the root of the inflated XML file.

一般在adapter的getview方法中使用View.inflate的比较多,最近在研究自定义下拉刷新listview的时候,使用了View.inflate一直报如下错误:

  1. mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  2. shView = (RelativeLayout) mInflater.inflate(R.layout.pull_to_refresh_header, this, false);
  3. //mRefreshView = (RelativeLayout)View.inflate(context, R.layout.pull_to_refresh_header, null);
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mRefreshView = (RelativeLayout) mInflater.inflate(R.layout.pull_to_refresh_header, this, false);
//mRefreshView = (RelativeLayout)View.inflate(context, R.layout.pull_to_refresh_header, null);
  1. android.view.InflateException: Binary XML file line #10: Error inflating class com.markupartist.android.widget.PullToRefreshListView
android.view.InflateException: Binary XML file line #10: Error inflating class com.markupartist.android.widget.PullToRefreshListView

而使用 LayoutInflater 就没事,原因翻阅很多资料都没弄明白,看样子定义控件的inflate还是使用

LayoutInflater 吧!

LayoutInflater 类的使用的更多相关文章

  1. LayoutInflater类详解

    http://www.cnblogs.com/top5/archive/2012/05/04/2482328.html   在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于 ...

  2. [Android FrameWork 6.0源码学习] LayoutInflater 类分析

    LayoutInflater是用来解析XML布局文件,然后生成对象的ViewTree的工具类.是这个工具类的存在,才能让我们写起Layout来那么省劲. 我们接下来进去刨析,看看里边的奥秘 //调用i ...

  3. Android LayoutInflater 类分析

    作为一名Android开发者,写页面是最普通不过的事情了,在编写页面的时候,系统给提供了两种形式,一种形式是通过XML的方式进行编写,还有一种形式是通过Java代码直接编写   我们知道Android ...

  4. 浅谈 LayoutInflater

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

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

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

  6. Android中将xml布局文件转化为View树的过程分析(下)-- LayoutInflater源码分析

    在Android开发中为了inflate一个布局文件,大体有2种方式,如下所示: // 1. get a instance of LayoutInflater, then do whatever yo ...

  7. LayoutInflater和inflate()

    LayoutInflater LayoutInflater抽象类是用来加载XML布局文件(UI界面)的. 作用: 1.对于一个没有被载入或者想要动态载入的界面,都需要使用LayoutInflater. ...

  8. Android LayoutInflater.inflate()的参数及其用法

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

  9. View inflate方法和LayoutInflater inflate方法的区别详解

    原创文章,转载请注明出处:http://www.cnblogs.com/baipengzhan/p/6257510.html 我们在Android开发中,对于将布局填充成View对象,最常用的两种办法 ...

随机推荐

  1. A. Launch of Collider Codeforces Round #363 (Div2)

    A. Launch of Collider time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  2. 转:JDBC驱动配置相关

    1.做JDBC请求 ,首先要了解这个JDBC对象是什么,现在以SQLServer为例来说明 首先下载对应的数据库驱动(百度“jdbc sqlserver驱动”,然后下载). 注意 :下载完成后,直接把 ...

  3. Hive基础学习

    Hive 学习记录Hive介绍:Hive 是起源于Facebook,使得Hadoop进行SQL查询成为可能,进而使得非程序员也可以进进行对其使用:它是一种数据仓库工具,将结构化的数据文件 映射为一张数 ...

  4. 【读书笔记】Linux源码注释

    第二章 大概的内部组成 IO端口寻址: 统一寻址: 就是把地址归入存储器寻址范围. 独立寻址: 跟存储器分开,专门的寻址空间 没怎么理解, PC机一般都是采用独立寻址, 见下图 在linux里,可以在 ...

  5. Chapter 2 Open Book——5

    I was relieved that I had the desk to myself, that Edward was absent. 我能一个人一张桌子很开心,就因为Edward 没来. I t ...

  6. 1.Java为什么能跨平台运行?请简述原理

    因为它有虚拟机(JVM),JAVA程序不是直接在电脑上运行的,是在虚拟机上进行的,每个系统平台都是有自己的虚拟机(JVM),所以JAVA语言能跨平台. 1, java代码不是直接运行在CPU上,而是运 ...

  7. 用ios做的一个简单的记事本

    #import "ViewController.h" @interface ViewController ()@property (weak, nonatomic) IBOutle ...

  8. Ubuntu DNS bind9 配置

    下面的配置就是实现解析test.zp.com到不同的IP地址 安装dns server软件包$ apt-get install bind9 配置dns配置文件的路径在/etc/bind路径下面添加一个 ...

  9. centos中apache-tomcat的配置

    在centos中配置Apache-toncat需要先安装jdk,前面文章已经写了怎么配置jdk,这里略过. 首先到官网下载好Apache-tomcat安装包,我这里下载的是apache-tomcat- ...

  10. 数据库连接池c3p0的设置

    spring-hibernate.xml配置 <?xml version="1.0" encoding="UTF-8"?> <beans xm ...