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()的更多相关文章

  1. 转载《 LayoutInflater 的inflate函数用法详解》

    很多人在网上问LayoutInflater类的用法,以及inflate()方法参数的含义,现解释如下: inflate()的作用就是将一个用xml定义的布局文件查找出来,注意与findViewById ...

  2. View.inflate和LayoutInflater的inflate方法区别

    平时ListView加载item中,adapter的getView方法中,我们经常用到: LayoutInflater.from(mContext).inflate(R.layout.it ,pare ...

  3. 三个案例带你看懂LayoutInflater中inflate方法两个参数和三个参数的区别

    关于inflate参数问题,我想很多人多多少少都了解一点,网上也有很多关于这方面介绍的文章,但是枯燥的理论或者翻译让很多小伙伴看完之后还是一脸懵逼,so,我今天想通过三个案例来让小伙伴彻底的搞清楚这个 ...

  4. Android编程之LayoutInflater的inflate方法具体解释

    LayoutInflater的inflate方法,在fragment的onCreateView方法中经经常使用到: public View onCreateView(LayoutInflater in ...

  5. 转载 LayoutInflater的inflate函数用法详解

    http://www.open-open.com/lib/view/open1328837587484.html LayoutInflater的inflate函数用法详解 LayoutInflater ...

  6. Android编程之LayoutInflater的inflate方法实例

    假设你不关心其内部实现,仅仅看怎样使用的话,直接看这篇就可以. 接上篇,接下来,就用最最简单的样例来说明一下: 用两个布局文件main 和 test: 当中,main.xml文件为: <?xml ...

  7. LayoutInflater和inflate的用法,有图有真相

    1.概述 有时候在我们的Activity中用到别的layout,并且要对其组件进行操作,比如: A.acyivity是获取网络数据的,对应布局文件为A.xml,然后需要把这个数据设置到B.xml的组件 ...

  8. Android编程之LayoutInflater的inflate方法详解

    LayoutInflater的inflate方法,在fragment的onCreateView方法中经常用到: public View onCreateView(LayoutInflater infl ...

  9. 带你看懂LayoutInflater中inflate方法

    关于inflate问题,我想很多人多多少少都了解一点,网上也有很多关于这方面介绍的文章,但是枯燥的理论或者翻译让很多小伙伴看完之后还是一脸懵逼,so,我今天想通过三个案例来让小伙伴彻底的搞清楚这个东东 ...

  10. LayoutInflater的inflate函数用法详解

    LayoutInflater作用是将layout的xml布局文件实例化为View类对象. 获取LayoutInflater的方法有如下三种: LayoutInflater inflater=(Layo ...

随机推荐

  1. Eclipse Memory Analyzer,内存泄漏插件,安装使用一条龙

    网上文档很多,但最初都有问题.整理一份,作为备份.使用过程:开发代码写完后,对可能出现内存溢出的代码,添加配置文件,生成.hprof文件,用memory Analyzer分析排查问题,且泄漏内存大小可 ...

  2. AngularJs转换json日期/Date(00000)/

    //过滤器ngApp.filter('jsonDate', function ($filter) { return function (input, format) { var timestamp = ...

  3. [SLAM]2D激光扫描匹配方法

    1.Beam Model 2.Likehood field for k=1:size(zt,1) if zt(k,2)>0 d = -grid_dim/2; else d = grid_dim/ ...

  4. 字节流和字符流(InputStream类和OutputStream类)

    java流包括字节流和字符流,字节流通过I/O设备以字节数据的方式读入,而字符流则是通过字节流读入数据转换成字符"流"的形式由用户驱使. InputStream是所有字节输入流的父 ...

  5. RDIFramework.NET ━ 9.11 数据字典管理 ━ Web部分

    RDIFramework.NET ━ .NET快速信息化系统开发框架 9.11  数据字典管理 -Web部分  数据字典模块主要对框架所需数据字典(即选项数据)进行管理,整个数据字典数据为框架所共享, ...

  6. LaTex学习笔记——LaTeX公式换行

    ps.  1.“\!” 表示其前后字符之间无间隙        2.暂留        3.段落中的数学表达式应该置于( 和), $ 和$ 或者begin{math} 和end{math} 之间.   ...

  7. CPlus播放多媒体之播放声音

    1.头文件需要<mmsystem.h>,但是之前需要包含<windows.h> 2.预处理#pragma comment<lib,"winmm.h"& ...

  8. gradle基础的build文件模板_jetty

    group '组织名' version '版本号' /* 支持的插件 */ apply plugin: 'java' // 项目基础变成语言支持为java apply plugin: 'war' // ...

  9. 在linux中访问virtualbox的共享文件夹

    1.在客户机里需要安装Virtualbox的增强功能. 2.使用virtualbox的图形界面设置好共享文件夹. 3.假设你设置的共享文件夹的名称是 share,使用如下命令在客户机的linux系统中 ...

  10. Java连接Redis (key-value存储系统)

    Redis简介: redis是一个性能非常优秀的内存数据库,通过key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表). ...