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 ...
随机推荐
- DHCP中继
DHCP中继 要求: 假设公司现在有两个部门分别为 销售部门,生产部门 对这两个部门分配不同网段, 销售部门 192.168.1.0/24 生产部门 192.168.2.0/24 为了节约使用LINU ...
- Progressive Scanning (逐行扫描) vs Interlaced Scanning (隔行扫描)
source: http://sh.sina.com.cn/20041207/231443445.shtml 逐行扫描每一帧图像均是由电子束顺序地一行接着一行连续扫描而成.要得到稳定的逐行扫描图像,每 ...
- VS2010的Razor智能感知和语法高亮突然消失
猜想可能是安装了VS2008的原因,尝试重新安装下面的组件,看看是否解决问题: 用于 Visual Studio 2010 SP1 和 Visual Web Developer 2010 SP1 的 ...
- thttpd和cgilua安装与运行流程分析
安装 参考如下博文安装thttpd软件 http://blog.csdn.net/21aspnet/article/details/7045845 http://blog.csdn.net/drago ...
- svg学习(八)polyline
<polyline> 标签用来创建仅包含直线的形状. <?xml version="1.0" standalone="no"?> < ...
- 修改远程桌面端口号.bat
@color 0A @title 修改远程桌面端口号 by wjshan0808 @echo off echo 请输入端口号 set /p port= reg add "HKLM\SYSTE ...
- Form 数据库的顺序
form buidler 中数据库的上下排列顺序也很重要,开发中无心把LINE的数据块放在HEADER数据块之上, 导致在新增数据的时候先去insert LINE数据块(没有生成HEADER_ID的 ...
- 分列:将excel单元格的内容拆分为两列
提要:处理excel数据时有时需要把单元格的内容拆分为两列,可能方便外部软件的链接,可能使数据显示更明晰等等,有人说直接剪切加粘贴不就可以了吗,但是有时数据过多,这样处理很不效率,网上搜索的方法说插入 ...
- 微信开发那点事(No.001)
MXS&Vincene ─╄OvЁ &0000024─╄OvЁ MXS&Vincene MXS&Vincene ─╄OvЁ:今天很残酷,明天更残酷,后天很美好, ...
- checkbox实现单选,全选,反选,取消选
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools=&q ...