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

方法2:

由服务获取:

  1. LayoutInflater inflater = (LayoutInflater)context.getSystemService
  2. (Context.LAYOUT_INFLATER_SERVICE);
  1. LayoutInflater inflater = (LayoutInflater)context.getSystemService
  2. (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等)。

Android中LayoutInflater的使用的更多相关文章

  1. Android 中 LayoutInflater 的使用

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

  2. android中LayoutInflater详解与使用

    android的LayoutInflater用来得到一个布局文件,也就是xxx.xml,而我们常用的findviewbyid是用来取得布局文件里的控件或都布局.inflater即为填充的意思,也就是说 ...

  3. Android高手进阶教程(五)之----Android 中LayoutInflater的使用!

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://weizhulin.blog.51cto.com/1556324/311450 大 ...

  4. Android中LayoutInflater()方法

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

  5. android中LayoutInflater.from(context).inflate的分析

    在应用中自定义一个view,需要获取这个view的布局,需要用到 (LinearLayout) LayoutInflater.from(context).inflate(R.layout.conten ...

  6. Android中measure过程、WRAP_CONTENT详解以及 xml布局文件解析流程浅析

    转自:http://www.uml.org.cn/mobiledev/201211221.asp 今天,我着重讲解下如下三个内容: measure过程 WRAP_CONTENT.MATCH_PAREN ...

  7. Android中使用ExpandableListView实现微信通讯录界面(完善仿微信APP)

    之前的博文<Android中使用ExpandableListView实现好友分组>我简单介绍了使用ExpandableListView实现简单的好友分组功能,今天我们针对之前的所做的仿微信 ...

  8. Android中ListView实现图文并列并且自定义分割线(完善仿微信APP)

    昨天的(今天凌晨)的博文<Android中Fragment和ViewPager那点事儿>中,我们通过使用Fragment和ViewPager模仿实现了微信的布局框架.今天我们来通过使用Li ...

  9. Android中Fragment和ViewPager那点事儿(仿微信APP)

    在之前的博文<Android中使用ViewPager实现屏幕页面切换和引导页效果实现>和<Android中Fragment的两种创建方式>以及<Android中Fragm ...

随机推荐

  1. VS2013中设置大小写的快捷键

    1.我们在定义头文件时,通常需要定义: #ifndef  _MainMenu_H_#define  _MainMenu_H_ your code... #endif 我们需要将头文件名设置为大写的: ...

  2. .net如何调试dll

    引用"谢绝关注的BLOG" http://blog.sina.com.cn/s/blog_50cc0ffd0100cqhp.html 在同一个解决方案中一定要将调用DLL文件的工程 ...

  3. Qt postEvent

    Qt3中可以直接向线程发送消息 QThread::postEventQ4中已不支持为了模拟向线程发送消息,可以通过QObject::moveToThread后,然后再向这个QObject发送消息 ob ...

  4. MYSQL存储过程实现in传入参数 where in('1','2')

    android 服务器端开发中遇到这么一个问题: 突然发现将字符串传入到存储过程,参数为 '1','2'  ,竟然执行无效 所以看到网上有在存储过程中直接拼凑sql的代码,今天也试了一下,可以执行了, ...

  5. win7旗舰版安装office2007后打开文件提示找不到proplusww.msi

    今天第一次打开2007的excel,出现错误如下: 解决办法: 转自:http://blog.163.com/huacai9420@126/blog/static/521585422011911524 ...

  6. 使用Forms进行身份验证(Asp.net)

    1.背景      以往项目登陆后的用户信息都是存放在session中,但session有一个问题就是读取的时候需要先实例化所在类,在调用对象()如果用static修饰,则可能到时多次登陆sessio ...

  7. JavaScript跨域总结与解决办法(转)

    JavaScript出于安全方面的考虑,不允许跨域调用其他页面的对象.但在安全限制的同时也给注入iframe或是ajax应用上带来了不少麻烦.这里把涉及到跨域的一些问题简单地整理一下: 首先什么是跨域 ...

  8. iOS开发之深入探讨runtime机制02-runtime的简单使用

    runtime机制为我们提供了一系列的方法让我们可以在程序运行时动态修改类.对象中的所有属性.方法. 下面就介绍运行时一种很常见的使用方式,字典转模型.当然,你可能会说,“我用KVO直接 setVal ...

  9. spoj 147

    dfs枚举真值 #include <cstdio> #include <cstring> #include <cstdlib> #include <stack ...

  10. 一个Java程序员应该掌握的10项技能

    1.语法:必须比较熟悉,在写代码的时候IDE的编辑器对某一行报错应该能够根据报错信息知道是什么样的语法错误并且知道任何修正. 2.命令:必须熟悉JDK带的一些常用命令及其常用选项,命令至少需要熟悉:a ...