Android中LayoutInflater的使用
Inflater英文意思是膨胀,在Android中应该是扩展的意思吧。
LayoutInflater
的作用类似于 findViewById(),不同点是LayoutInflater是用来找layout文件夹下的xml布局文件,并且实例化!而
findViewById()是找具体某一个xml下的具体 widget控件(如:Button,TextView等)。
获取它的用法有3种:
方法1:
由LayoutInflater的静态函数:from(Context context) 获取:
static LayoutInflater from(Context context);
如:
- LayoutInflater inflater = LayoutInflater.from(this);
- View view=inflater.inflate(R.layout.ID, null);
- //或写成:
- 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:
由服务获取:
- LayoutInflater inflater = (LayoutInflater)context.getSystemService
- (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等)。
Android中LayoutInflater的使用的更多相关文章
- Android 中 LayoutInflater 的使用
一.LayoutInflater 的作用 我们一般使用 LayoutInflater 做一件事:View inflate(int resource, ViewGroup root); inflate( ...
- android中LayoutInflater详解与使用
android的LayoutInflater用来得到一个布局文件,也就是xxx.xml,而我们常用的findviewbyid是用来取得布局文件里的控件或都布局.inflater即为填充的意思,也就是说 ...
- Android高手进阶教程(五)之----Android 中LayoutInflater的使用!
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://weizhulin.blog.51cto.com/1556324/311450 大 ...
- Android中LayoutInflater()方法
在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById().不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例 ...
- android中LayoutInflater.from(context).inflate的分析
在应用中自定义一个view,需要获取这个view的布局,需要用到 (LinearLayout) LayoutInflater.from(context).inflate(R.layout.conten ...
- Android中measure过程、WRAP_CONTENT详解以及 xml布局文件解析流程浅析
转自:http://www.uml.org.cn/mobiledev/201211221.asp 今天,我着重讲解下如下三个内容: measure过程 WRAP_CONTENT.MATCH_PAREN ...
- Android中使用ExpandableListView实现微信通讯录界面(完善仿微信APP)
之前的博文<Android中使用ExpandableListView实现好友分组>我简单介绍了使用ExpandableListView实现简单的好友分组功能,今天我们针对之前的所做的仿微信 ...
- Android中ListView实现图文并列并且自定义分割线(完善仿微信APP)
昨天的(今天凌晨)的博文<Android中Fragment和ViewPager那点事儿>中,我们通过使用Fragment和ViewPager模仿实现了微信的布局框架.今天我们来通过使用Li ...
- Android中Fragment和ViewPager那点事儿(仿微信APP)
在之前的博文<Android中使用ViewPager实现屏幕页面切换和引导页效果实现>和<Android中Fragment的两种创建方式>以及<Android中Fragm ...
随机推荐
- MarkdownPad2添加目录(输出为HTML时可用)
平时看书的时候懒得上网写在线博客,就在电脑上用了很长时间的MarkDownPad2来记录自己的心得笔记,等那天高兴了再把他们贴出来.界面清爽,是我使用它最重要的原因,但是MarkdownPad2导出的 ...
- 后缀树(Suffix Tree)
问题描述: 后缀树(Suffix Tree) 参考资料: http://www.cppblog.com/yuyang7/archive/2009/03/29 ...
- 2014 Multi-University Training Contest 6
官方解题报告:http://blog.sina.com.cn/s/blog_a19ad7a10102uz2v.html Apple Tree http://acm.hdu.edu.cn/showpro ...
- Oracle session连接数和inactive的问题记录
Oracle session连接数和inactive的问题记录 http://timnity.javaeye.com/blog/280383 从上周起,服务器Oracle数据库出现问题,用不到半天,就 ...
- Observer Pattern
Motivation We can not talk about Object Oriented Programming without considering the state of the ob ...
- android音乐播放器开发教程
android音乐播放器开发教程 Android扫描sd卡和系统文件 Android 关于录音文件的编解码 实现米聊 微信一类的录音上传的功能 android操作sdcard中的多媒体文件——音乐列表 ...
- REST_FRAMEWORK加深记忆-三种CLASS VIEW的进化史
一层一层的封装,又能到底层,就会有全局感啦... from rest_framework import status from rest_framework.response import Respo ...
- Ubuntu环境下手动配置ElasticSearch0.90.5
1 下载elasticsearch-0.90.5 2 修改配置(可选) 修改内存:(可选) bin/elasticsearch.in.sh中: ES_MIN_MEM ES_MAX_MEM 修改搜索引擎 ...
- 03 - 运行OCCI测试程序遇到0xc0150002错误
通过控制面板->管理工具->Event Viewer->Windows Log->Application Logs, 可以看到可以看到是如下错误 “C:\Windows\ora ...
- 套题T7
P4712 铺瓷砖 时间: 1000ms / 空间: 65536KiB / Java类名: Main 描述