LayoutInflater和inflate()
LayoutInflater
LayoutInflater抽象类是用来加载XML布局文件(UI界面)的.
作用:
1、对于一个没有被载入或者想要动态载入的界面,都需要使用LayoutInflater.inflate()或View.inflate()来载入,然后也可以通过findByIdView()获取界面的元素;
2、对于一个已经通过setContentView()载入的界面,就可以使用findViewById()方法来获得其中的界面元素。
获得 LayoutInflater 实例的三种方式 (inflate()方法的作用看后面)
1. LayoutInflater inflater = getLayoutInflater(); //调用Activity的getLayoutInflater()
例:View view = getLayoutInflater().inflate(R.layout.Setting_view, null);//加载R.layout.Setting_view布局
2. LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view=inflater.inflate(R.layout.Setting_view, null);//加载R.layout.Setting_view布局
3. LayoutInflater inflater = LayoutInflater.from(context);
例:View view = LayoutInflater.from(mContext).inflate(R.layout.Setting_view, null);//加载R.layout.Setting_view布局
View.inflate()方法
View类和LayoutInflater类都有该方法。但两个类中的该方法作用是一样的,都是用来加载布局的。
inflate()的作用是加载一个用XML定义的布局文件(UI界面),而后可以使用findViewById()查找该布局文件内的控件,
而直接使用findViewById则只能从主布局文件(setContentView(R.layout.activity_main)中的R.layout.activity_main布局文件)
中查找控件。
用View.inflate()加载布局
infalte()加载布局文件
View v= View.inflate(this,R.layout.setting_view,null);
textView = (TextView) v.findViewById(R.id.text);
textView.setText("Hello");
R.layout.setting_view布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/text"
android:textColor="@color/colorAccent"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
参考:http://blog.csdn.net/lovexieyuan520/article/details/9036673
http://blog.csdn.net/it1039871366/article/details/9796965
深入了解:http://blog.csdn.net/guolin_blog/article/details/12921889
http://bxbxbai.github.io/2014/11/19/make-sense-of-layoutinflater/
LayoutInflater和inflate()的更多相关文章
- 转载《 LayoutInflater 的inflate函数用法详解》
很多人在网上问LayoutInflater类的用法,以及inflate()方法参数的含义,现解释如下: inflate()的作用就是将一个用xml定义的布局文件查找出来,注意与findViewById ...
- View.inflate和LayoutInflater的inflate方法区别
平时ListView加载item中,adapter的getView方法中,我们经常用到: LayoutInflater.from(mContext).inflate(R.layout.it ,pare ...
- 三个案例带你看懂LayoutInflater中inflate方法两个参数和三个参数的区别
关于inflate参数问题,我想很多人多多少少都了解一点,网上也有很多关于这方面介绍的文章,但是枯燥的理论或者翻译让很多小伙伴看完之后还是一脸懵逼,so,我今天想通过三个案例来让小伙伴彻底的搞清楚这个 ...
- Android编程之LayoutInflater的inflate方法具体解释
LayoutInflater的inflate方法,在fragment的onCreateView方法中经经常使用到: public View onCreateView(LayoutInflater in ...
- 转载 LayoutInflater的inflate函数用法详解
http://www.open-open.com/lib/view/open1328837587484.html LayoutInflater的inflate函数用法详解 LayoutInflater ...
- Android编程之LayoutInflater的inflate方法实例
假设你不关心其内部实现,仅仅看怎样使用的话,直接看这篇就可以. 接上篇,接下来,就用最最简单的样例来说明一下: 用两个布局文件main 和 test: 当中,main.xml文件为: <?xml ...
- LayoutInflater和inflate的用法,有图有真相
1.概述 有时候在我们的Activity中用到别的layout,并且要对其组件进行操作,比如: A.acyivity是获取网络数据的,对应布局文件为A.xml,然后需要把这个数据设置到B.xml的组件 ...
- Android编程之LayoutInflater的inflate方法详解
LayoutInflater的inflate方法,在fragment的onCreateView方法中经常用到: public View onCreateView(LayoutInflater infl ...
- 带你看懂LayoutInflater中inflate方法
关于inflate问题,我想很多人多多少少都了解一点,网上也有很多关于这方面介绍的文章,但是枯燥的理论或者翻译让很多小伙伴看完之后还是一脸懵逼,so,我今天想通过三个案例来让小伙伴彻底的搞清楚这个东东 ...
- LayoutInflater的inflate函数用法详解
LayoutInflater作用是将layout的xml布局文件实例化为View类对象. 获取LayoutInflater的方法有如下三种: LayoutInflater inflater=(Layo ...
随机推荐
- AFNetWorking 队列请求
我们在开发过程中,经常会遇到有些页面不止一个网络请求,有时候需要两个三个甚至更多,这个时候我们就需要队列请求,下边是GET请求的多个请求放在队列里边: NSURL *url = [NSURL URLW ...
- EF CodeFirst 创建数据库
最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来. 十年河东十年河西,莫欺少年穷 学无止境,精益求精 话说EF支持三种模式:Code First M ...
- Python 字符串方法详解
Python 字符串方法详解 本文最初发表于赖勇浩(恋花蝶)的博客(http://blog.csdn.net/lanphaday),如蒙转载,敬请保留全文完整,切勿去除本声明和作者信息. ...
- PHP---------PHP函数里面的static静态变量
工作一年了,一年里很少用到static这个关键词,不管是类里面还是方法里面基本都没怎么用过.平时看到类里面有这个都没什么好奇的,今天在函数里面看到了这个,就去百度了一下. <?phpfuncti ...
- Cocos2d-x PluginX (一)使用手册
简介 Plugin-x是cocos2d-x最近版本中引入的特性,旨在解决第三方SDK接入的问题,最大程度的简化接入工作,增加可重用性.第一部分主要介绍PluginX如何使用,不涉及设计原理相关内容. ...
- Sie sind das Essen und wir sind die Jaeger!
WCF http://www.cnblogs.com/iamlilinfeng/archive/2012/09/25/2700049.html HTTP 数据库分库分表 读写分离 负载均衡 wind ...
- z/OS上Dataset 的移动
最近的一个需求,需要把大批量的Dataset移到新的Storage Class,新的Volume中去,刚开始感觉非常头疼.仔细研究后发现这个事情其实很简单.确实符合别人所说,事情的在你真正开始努力之后 ...
- LNK1169: one or more multiply defined symbols found
The build failed due to multiple definitions of one or more symbols. This error is preceded by error ...
- 助手系列之连接mysql数据库
import MySQLdbdef main(): try: conn=MySQLdb.connect(host='127.0.0.1',user='root',passwd='pass',db='a ...
- SQLDMO知识总结
SQLDMO(SQL Distributed Management Objects) 参考手册:http://technet.microsoft.com/en-us/library/aa312550( ...