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 ...
随机推荐
- Sql Server Express连接字符串
data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\ASPNETDB.MDF;User ...
- react js 按条数 展开/折叠
//2条const MAX_SHOW_NUM = 2; class HotDiscuss extends Component { static propTypes = { //验证 repliedCo ...
- mysql的一些基本操作语句
-- 创建一个php2016的数据库create database php2016;-- 查看数据库的创建创建语句show create database php2016;-- 指定默认的操作数据库u ...
- @Autowired获取被@Service注解的bean为null的问题
先说结论:Spring容器还没有加载完Bean,你就去调用了! 一般的注解没加,bean的名字写错都好检查,但是逻辑错误就需要看清自己的思维过程了. 实例:在使用ActiveMq的过程中,第一步对Ac ...
- 刨根问底U3D---Vector3 你到底是蔬菜呢还是水果呢?
事情的起因还是因为一段代码,因为在做一个2D TileBase的游戏 所以需要有一个简单的 Tile坐标到世界坐标的变换 public static Vector3 GetTileWorldPosBy ...
- CPlus播放多媒体之播放声音
1.头文件需要<mmsystem.h>,但是之前需要包含<windows.h> 2.预处理#pragma comment<lib,"winmm.h"& ...
- 1046: 最小的K个数
1046: 最小的K个数 时间限制: 1 Sec 内存限制: 128 MB提交: 233 解决: 200[提交][状态][讨论版] 题目描述 输入n个整数,找出其中最小的K个数.例如输入4,5,1 ...
- ELK修炼之道
看了ELK大半年了,现在就慢慢的总结一下对ELK的理解 参考资料 ELK stack中文指南 Elasticsearch权威指南 官方文档 Elasticsearch基础篇 此篇用于介绍Elastic ...
- Hibernate Criteria Restrictions
HQL运算符 QBC运算符 含义 = Restrictions.eq() 等于equal <> Restrictions.ne() 不等于not equal > Restrict ...
- php将抓取的图片链接下载到本地
备注: fclose这一步操作完成之后如果返回值是空表示下载成功,否则会返回1或者其他错误提示 这个步骤测试的时候实在yii2框架的基础上执行的,下载到的图片实在frontend目录下的web目录下( ...