[转]LayoutInflater的inflate函数用法
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函数用法的更多相关文章
- 转载 LayoutInflater的inflate函数用法详解
http://www.open-open.com/lib/view/open1328837587484.html LayoutInflater的inflate函数用法详解 LayoutInflater ...
- 转载《 LayoutInflater 的inflate函数用法详解》
很多人在网上问LayoutInflater类的用法,以及inflate()方法参数的含义,现解释如下: inflate()的作用就是将一个用xml定义的布局文件查找出来,注意与findViewById ...
- LayoutInflater的inflate函数用法详解
LayoutInflater作用是将layout的xml布局文件实例化为View类对象. 获取LayoutInflater的方法有如下三种: LayoutInflater inflater=(Layo ...
- LayoutInflater的inflate函数用法
LayoutInflater作用是将layout的xml布局文件实例化为View类对象. 获取LayoutInflater的方法有如下三种: LayoutInflater inflater=(Layo ...
- LayoutInflater和inflate的用法,有图有真相
1.概述 有时候在我们的Activity中用到别的layout,并且要对其组件进行操作,比如: A.acyivity是获取网络数据的,对应布局文件为A.xml,然后需要把这个数据设置到B.xml的组件 ...
- Android成长之路-LayoutInflater和inflate的用法
在这里用Tabhost的例子来说明: package cn.csdn.activity; import android.app.TabActivity; import android.os.Bundl ...
- LayoutInflater和inflate()方法的用法
LayoutInflater作用是将layout的xml布局文件实例化为View类对象. 实现LayoutInflater的实例化共有3种方法, (1).通过SystemService获得 Layou ...
- getViewById和getLayoutInflater().inflate的用法
getViewById和getLayoutInflater().inflate得用法 1.什么是LayoutInflaterThis class is used to instantiate layo ...
- LayoutInflater和inflate()方法的使用方法
public static LayoutInflaterfrom(Context context) { LayoutInflaterLayoutInflater = (LayoutInflater)c ...
随机推荐
- ElasticSearch(3)-原理
参考文档: http://learnes.net/distributed_crud/bulk_requests.html 一.分布式集群 1.1 空集群 单台机器,其中没有数据,也没有索引. 集群中一 ...
- JSP table中除了第一行(标题)其他全清空
表格如下 <div style="overflow-x: auto;width: 800px"> <table id="taresults02" ...
- Leetcode016 3Sum Closest
public class S016 { //借鉴S015的思想,只是稍微有点慢 public int threeSumClosest(int[] nums, int target) { Arrays. ...
- hdu 1028 Ignatius and the Princess III(母函数入门+模板)
Description "Well, it seems the first problem is too easy. I will let you know how foolish you ...
- 第四题 (List)写一个函数reverseList,该函数能够接受一个List,然后把该List 倒序排列。 例如: List list = new ArrayList(); list.add(“Hello”); list.add(“World”); list.add(“Learn”); //此时list 为Hello World Learn r
package zuoye; import java.util.ArrayList; import java.util.List; public class Reverse01 { public st ...
- Python in minute
Python 性能优化相关专题: https://www.ibm.com/developerworks/cn/linux/l-cn-python-optim/ Python wikipedi ...
- 笨方法学python--参数,解包,变量
1 cmd中执行 python ex11.py, ex11.py部分也是所谓的"参数". 2 from sys import argv script, first, second, ...
- apache与php安装
安装库文件 yum install -y pcre pcre-devel apr apr-deve 安装apache cd /usr/local/src/httpd-2.4.23 Bundled AP ...
- 第三十一节,time时间模块
模块简介 模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能需要 ...
- Codeforces Round #364 (Div. 2) D. As Fast As Possible
D. As Fast As Possible time limit per test 1 second memory limit per test 256 megabytes input stand ...