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. request.getHeader所想到的

    request.getHeader(""),简单的说就是获取请求的头部信息,根据http协议,它能获取到用户访问链接的信息. /** * Returns the value of ...

  2. Android中取消GridView & ListView默认的点击背景色

    方法一: gridView.setSelector(new ColorDrawable(Color.TRANSPARENT)); listView.setSelector(new ColorDrawa ...

  3. Android布局

    android:gravity="center" android:orientation="vertical" android:orientation=&quo ...

  4. geckodriver v0.11.0 github上下载的

    从 https://github.com/mozilla/geckodriver/releases 下载的.家里下载不了,selenium 3.0.1 + python 2.7.10 + firefo ...

  5. zTree插件之单选下拉菜单代码

    <!--ztree树结构--> <link rel="stylesheet" type="text/css" href="asset ...

  6. 白话LINQ系列2---以代码演进方式学习LINQ必备条件

    今天,我们通过一个简单的示例代码的演进过程,来学习LINQ必备条件:隐式类型局部变量:对象集合初始化器:委托:匿名函数:lambda表达式:扩展方法:匿名类型.废话不多说,我们直接进入主题. 一.实现 ...

  7. 【C解毒】怎样写main()函数

    [C解毒]怎样写main()函数(出处: CUNIX论坛)

  8. Java中的4种代码块

    一.普通代码块 直接在一个方法中出现的{}就称为普通代码块,例子程序如下: public class CodeDemo01{ public static void main(String[] args ...

  9. SQL2008根据日志恢复

    --创建测试数据库 CREATE DATABASE Db GO --对数据库进行备份 BACKUP DATABASE Db TO DISK='c:\db.bak' WITH FORMAT GO --创 ...

  10. QTP

    QTP支持功能测试和回归测试的自动化. 支持录制功能,可以对脚本设置检查点,也可以进行参数化.可以在一个ACTION中调用另外一个ACTION action可以调用外部脚本.可以复制已有的ACTION ...