一、inflate简介

inflate这个方法总共有四种形式(见下面),目的都是把xml表述的layout转化为View对象。

其中有一个比较常用,View inflate(int resource, ViewGroup root),另三个,其实目的和这个差不多。

int resource,也就是resource/layout文件在R文件中对应的ID,这个必须指定。

而ViewGroup root则可以是null,null时就只创建一个resource对应的View,不是null时,会将创建的view自动加为root的child。

二、setContentView和inflate区别:

setContentView()一旦调用, layout就会立刻显示UI。
而inflate只会把Layout形成一个以view类实现成的对象,有需要时再用setContentView(view)显示出来。
一般在activity中通过setContentView()将界面显示出来,但是如果在非activity中如何对控件布局设置操作了,这需LayoutInflater动态加载。
< TextView
android:id="@+id/tview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="ATAAW.COM"
/>
< Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/button"
android:text="按钮"
/>

在程序中动态加载以上布局。
LayoutInflater flater = LayoutInflater.from(this);
View view = flater.inflate(R.layout.example, null);
获取布局中的控件。
button = (Button) view.findViewById(R.id.button);
textView = (TextView)view.findViewById(R.id.tview);
***********************************************************

三、接下来结合源码说说inflate方法的四种形式:

inflate方法总共有四种形式,把xml表达的layout转化为view. This class is used to instantiate layout xml files into its corresponding view object. It is never be used directly——use getLayoutInflater() or getSystemService(String)getLayoutInflate() or getSystemService(String) to retrieve a standard LayoutInflater instance that is already hooked up that is already hook up to the current context and correct configured for the device you are running on. 
1. Context.public abstract object getSystemService(String name) 
2. 两种获得LayoutInflater的方法 
a. 通过SystemService获得 
LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLEATER_SERVICE); 
b. 从给定的context中获取 
Public static LayoutInflater from(Context context) 
c. 两者的区别:实际上是一样的,源码 
/** 
     * Obtains the LayoutInflater from the given context. 
     */ 
    public static LayoutInflater from(Context context) { 
        LayoutInflater LayoutInflater = 
                (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
        if (LayoutInflater == null) { 
            throw new AssertionError("LayoutInflater not found."); 
        } 
        return LayoutInflater; 

3. LayoutInflater.inflate()将Layout文件转换为View,专门供Layout使用的Inflater。虽然Layout也是View的子类,但在android中如果想将xml中的Layout转换为View放入.java代码中操作,只能通过Inflater,而不能通过findViewById()。 
4. LinearLayout linearLayout = 
(LinearLayout) findViewById(R.id.placeslist_linearlayout); 
linearLayout.addView(place_type_text); 
5. findViewById有两种形式 
R.layout.xx是引用res/layout/xx.xml的布局文件(inflate 方法),R.id.xx是引用布局文件里面的组件,组件的id是xx(findViewById方法)。所有的组件id都能用R.id.xx来查看,但是组件不在setContentView()里面的layout中就无法使用,Activity.findViewById()会出现空指针异常 
a. activity中的findViewById(int id) 
b. View 中的findViewById(int id) 
6.不同点是LayoutInflater是用来找layout下xml布局文件,并且实例化!而findViewById()是找具体xml下的具体 widget控件(如:Button,TextView等)。

inflate简介,LayoutInflater和inflate()方法的用法相关推荐

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

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

  2. LayoutInflater的inflate函数用法详解

    LayoutInflater的inflate函数用法详解 LayoutInflater作用是将layout的xml布局文件实例化为View类对象. 获取LayoutInflater的方法有如下三种: ...

  3. LayoutInflater中inflate()方法的学习使用

    LayoutInflater中inflate()方法的学习使用 一.LayoutInflater ​ LayoutInflater实例化一个xml布局加到对应的View对象中.该对象不直接使用.调用g ...

  4. 带你看懂LayoutInflater中inflate方法

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

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

    本文转载自[http://blog.csdn.net/u012702547/article/details/52628453] 关于inflate参数问题,我想很多人多多少少都了解一点,网上也有很多关 ...

  6. LayoutInflater的inflate方法使用

    Android动态添加布局时经常会用到LayoutInflater的inflate(int resource, ViewGroup root, boolean attachToRoot) 方法,但是i ...

  7. php中dump怎么使用,php中var_dump()方法的用法简介

    摘要 腾兴网为您分享:php中var_dump()方法的用法简介,支付旭尊,鑫钱袋,携程,小米音乐等软件知识,以及星票网,dc,新房购置税计算器,听中国app,xbox360,当游游戏盒,蜗牛二手货车 ...

  8. 直接法 matlab,解线性方程组直接方法matlab用法.doc

    解线性方程组直接方法matlab用法 在这章中我们要学习线性方程组的直接法,特别是适合用数学软件在计算机上求解的方法. 2.1 方程组的逆矩阵解法及其MATLAB程序 2.1.3 线性方程组有解的判定 ...

  9. PHP中魔术方法的用法

    PHP中魔术方法的用法 /** PHP把所有以__(两个下划线)开头的类方法当成魔术方法.所以你定义自己的类方法时,不要以 __为前缀. * */// __toString.__set.__get__ ...

最新文章

  1. 分享6 个值得收藏的 Python 代码
  2. 3399引擎_RK3399开发板 AIO-3399C六核高性能主板
  3. C++ Opengl 显示列表源码
  4. java中让数据生成excle文件并且支持下载
  5. W玻色子质量实验与理论矛盾,或是十年来最重要的进展
  6. 自学python-python自学难吗
  7. Android handleMessage和sendMessage 简单示例
  8. 无法登录苹果开发者_苹果开发者账号最新2020申请方式可支付宝微信付款
  9. EBS中二次开发FSG报表2(SQL)
  10. Ubuntu安装Burg
  11. 3dmax2022兼容疯狂模渲大师最新版|疯狂模渲大师3.6.0.4下载安装步骤教程怎么激活素材库和装机3dmax超一流辅助客户端的?
  12. android 获取wifi连接不上,如何检测无法在android中连接wifi?
  13. 区块链、NFT与元宇宙中的稀缺性技术
  14. 1、OpenSearch入门配置
  15. Goodbye,OI!
  16. Hibernate @Formula详解
  17. 再谈angularJS数据绑定机制及背后原理—angularJS常见问题总结
  18. 家装灯线走线图_求一套完整家庭装修电路图和走线图用来学习
  19. 学习Swift:经验丰富的开发人员指南
  20. 品类甚至远比品牌更能影响一个企业

热门文章

  1. 毕昇 JDK:“传奇再现”华为如何打造 ARM 上最好用的 JDK?
  2. 如何将Word与Endnote再次相互关联?
  3. 目标检测第1步:如何在Windows 10下安装MiniConda?
  4. 2014 网选 5011 Game(Nim游戏,数学题)
  5. (二)linux内核镜像制作
  6. pil python 安装_20行Python代码给微信头像戴帽子
  7. usb接口供电不足_1个USB接口变成4个?什么东西那么“牛”?请你花2分钟了解一下...
  8. nvidia-smi 命令详解
  9. java 两个页面传递数据,请问Cookie怎么在两个页面间传递数据?
  10. xbox手柄接收器驱动_xbox手柄连接 win10电脑