LayoutInflater的inflate方法,在fragment的onCreateView方法中经常用到:

  1. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  2. Bundle savedInstanceState) {

LayoutInflater的inflate方法一共有四种,但我们日常用经常用到的就只有这两种:

  1. public View inflate(int resource, ViewGroup root) {
  2. return inflate(resource, root, root != null);
  3. }
  1. public View inflate(int resource, ViewGroup root, boolean attachToRoot) {
  2. if (DEBUG) System.out.println("INFLATING from resource: " + resource);
  3. XmlResourceParser parser = getContext().getResources().getLayout(resource);
  4. try {
  5. return inflate(parser, root, attachToRoot);
  6. } finally {
  7. parser.close();
  8. }
  9. }

所以,这里直接介绍里面调用这个方法即可:

  1. public View inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot)

在这个方法里面,上半部分为xml解析的代码,这里就不贴出来,确实没什么东西可看。直接看中间部分的代码:

  1. ViewGroup.LayoutParams params = null;
  2. if (root != null) {
  3. if (DEBUG) {
  4. System.out.println("Creating params from root: " +
  5. root);
  6. }
  7. // Create layout params that match root, if supplied
  8. params = root.generateLayoutParams(attrs);
  9. if (!attachToRoot) {
  10. // Set the layout params for temp if we are not
  11. // attaching. (If we are, we use addView, below)
  12. temp.setLayoutParams(params);
  13. }
  14. }

params = root.generateLayoutParams(attrs);

这段的意思是:如果调用inflate方法,传入了ViewGroup root参数,则会从root中得到由layout_width和layout_height组成的LayoutParams,在attachToRoot设置为false的话,就会对我们加载的视图View设置该LayoutParams。

接着往下看:

  1. // We are supposed to attach all the views we found (int temp)
  2. // to root. Do that now.
  3. if (root != null && attachToRoot) {
  4. root.addView(temp, params);
  5. }
  6. // Decide whether to return the root that was passed in or the
  7. // top view found in xml.
  8. if (root == null || !attachToRoot) {
  9. result = temp;
  10. }

root.addView(temp, params);

如果设置了ViewGroup root参数,且attachToRoot设置为true的话,则将我们加载的视图做为子视图添加到root视图中。

如果我们ViewGroup root设置为空的话,就直接返回我们创建的视图;如果root不为空,且attachToRoot设置为false的话,就返回上面那段:对我们加载的视图View设置该LayoutParams。

以上就是该方法内容,可能你还有点看不太懂吧,下一篇文章,我将会做几个例子,来具体说明一下。

如果你不关心其内部实现,只看如何使用的话,直接看这篇即可。

接上篇,接下来,就用最最简单的例子来说明一下:

用两个布局文件main 和 test:

其中,main.xml文件为:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical" >
  6. <TextView
  7. android:layout_width="match_parent"
  8. android:layout_height="50dp"
  9. android:gravity="center"
  10. android:text="hello world" />
  11. </LinearLayout>

test.xml文件为:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="200dp"
  5. android:background="#ffffff00"
  6. android:orientation="vertical" >
  7. <TextView
  8. android:layout_width="match_parent"
  9. android:layout_height="50dp"
  10. android:gravity="center"
  11. android:text="test" />
  12. </LinearLayout>

在test中设置了其高度为200dp,并且设置了背景颜色。

接下来看一下LayoutInflater().inflate方法实现:

第一种方式:inflate(view, null)

  1. @Override
  2. protected void onCreate(Bundle savedInstanceState) {
  3. super.onCreate(savedInstanceState);
  4. View view = (LinearLayout) getLayoutInflater().inflate(R.layout.main,
  5. null);
  6. view = getLayoutInflater().inflate(R.layout.test, null);
  7. setContentView(view);
  8. }

运行的效果如下:

这个就很容易理解了,因为我没有指定ViewGroup root参数,所以,相当于直接加载了test视图文件,并返回。

而它的高度充满了全屏而不是200dp,因为执行inflate的时候,没有root参数,则无法为test视图设定layoutparam参数。那么为什么会充满屏幕而不是没有显示呢?是因为我们将其设置视图到activity时,会取得当前window的layoutparam赋值给它,也就是充满全屏。有兴趣的话,你可以改一下test的layout_width设定一个数值,最后运行效果是一样的。

第二种方式:inflate(view, root, false)

  1. @Override
  2. protected void onCreate(Bundle savedInstanceState) {
  3. super.onCreate(savedInstanceState);
  4. View view = (LinearLayout) getLayoutInflater().inflate(R.layout.main,
  5. null);
  6. view = getLayoutInflater().inflate(R.layout.test, (ViewGroup) view, false);
  7. setContentView(view);
  8. }

这里调用inflate的时候,强转了view为viewgroup,因为其本身就是linearlayout,所以这里可以强转。

运行的效果如下:

单看效果而言,跟上面的一样。但从代码本身而言,实现的内容就不一样了。由于有了viewgroup,这里得到的视图其实已经有了layoutparam,你可以自行打印Log看看。

但为什么最后的结果却是和上面的一样呢。原因还是由于设置视图到activity时,会取得当前window的layoutparam赋值给它,也就是充满全屏。

第三种方式:inflate(view, root, true)

  1. @Override
  2. protected void onCreate(Bundle savedInstanceState) {
  3. super.onCreate(savedInstanceState);
  4. View view = (LinearLayout) getLayoutInflater().inflate(R.layout.main,
  5. null);
  6. view = getLayoutInflater().inflate(R.layout.test, (ViewGroup) view,
  7. true);
  8. setContentView(view);
  9. }

运行的效果如下:

这个效果就很明显了,由于main是线性布局,所以,test视图被添加到了textview(hello world)下面,并且保留了其自己的layoutparam参数。

例子很简单,就不附上代码工程。

如果对inflate方法如何实现的,感兴趣的话,可以参考上一篇文章:

Android编程之LayoutInflater的inflate方法详解

补充:新的API会在inflater.inflate(R.layout.xxx, null);提示错误:

Android编程之LayoutInflater的inflate方法详解的更多相关文章

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

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

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

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

  3. 网络编程之TCP/IP各层详解

    网络编程之TCP/IP各层详解 我们将应用层,表示层,会话层并作应用层,从TCP/IP五层协议的角度来阐述每层的由来与功能,搞清楚了每层的主要协议,就理解了整个物联网通信的原理. 首先,用户感知到的只 ...

  4. linux网络编程之shutdown() 与 close()函数详解

    linux网络编程之shutdown() 与 close()函数详解 参考TCPIP网络编程和UNP: shutdown函数不能关闭套接字,只能关闭输入和输出流,然后发送EOF,假设套接字为A,那么这 ...

  5. android emulator启动的两种方法详解

    android emulator启动的两种方法详解    转https://blog.csdn.net/TTS_Kevin/article/details/7452237 对于android学习者,模 ...

  6. View (一)LayoutInflater()方法详解

    相信接 触Android久一点的朋友对于LayoutInflater一定不会陌生,都会知道它主要是用于加载布局的.而刚接触Android的朋友可能对 LayoutInflater不怎么熟悉,因为加载布 ...

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

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

  8. Android——onCreate( )方法详解(转)

    android开发之onCreate( )方法详解 onCreate( )方法是android应用程序中最常见的方法之一,那么,我们在使用onCreate()方法的时候应该注意哪些问题呢? 先看看Go ...

  9. Android View 的绘制流程之 Layout 和 Draw 过程详解 (二)

    View 的绘制系列文章: Android View 的绘制流程之 Measure 过程详解 (一) Android View 绘制流程之 DecorView 与 ViewRootImpl 在上一篇  ...

随机推荐

  1. 剑指offer-二叉搜索树的后序遍历序列23

    题目描述 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则输出Yes,否则输出No.假设输入的数组的任意两个数字都互不相同. class Solution: def Verif ...

  2. 关于mysql开元数据库的几个随想

    现在已经是凌晨了,昨天晚上写了我人生中的第一篇笔记,觉得没什么可写的,写了一个多小时都没写出什么,现在突然想写点东西了,这是一个比较有趣的问题,前两个月换了新工作,记得当初面试这份工作的时候面试到第三 ...

  3. Python3实现机器学习经典算法(四)C4.5决策树

    一.C4.5决策树概述 C4.5决策树是ID3决策树的改进算法,它解决了ID3决策树无法处理连续型数据的问题以及ID3决策树在使用信息增益划分数据集的时候倾向于选择属性分支更多的属性的问题.它的大部分 ...

  4. IntelliJ IDEA 2017.3/2018.1/.2 激活

    传统的License Server方式已经无法注册IntelliJ IDEA2017.3的版本了. http://idea.lanyus.com,这个网站有破解补丁和注册码两种方式,另外http:// ...

  5. vue.js学习之 跨域请求代理与axios传参

    vue.js学习之 跨域请求代理与axios传参 一:跨域请求代理 1:打开config/index.js module.exports{ dev: { } } 在这里面找到proxyTable{}, ...

  6. 软工冲刺-Alpha 冲刺 (3/10)

    队名:起床一起肝活队 组长博客:博客链接 作业博客:班级博客本次作业的链接 组员情况 组员1(队长):白晨曦 过去两天完成了哪些任务 描述: 很胖,刚学,照猫画虎做了登录与注册界面. 展示GitHub ...

  7. Where to go from here

    Did you get through all of that content? Congratulations! You've learnt the fundamentals of algorith ...

  8. 记一次dll强命名冲突事件

    一  问题的出现 现在要做一个net分布式平台,平台涉及多个服务之间调用问题,最基础的莫过于sso.由于我们的sso采用了wcf一套私有框架实现,另外一个webapi服务通过接口调用sso服务.由于s ...

  9. fcntl函数详解

    功能描述:根据文件描述词来操作文件的特性. 文件控制函数          fcntl -- file control头文件: #include <unistd.h> #include & ...

  10. 苹果ATS特性服务器配置指南 HTTPS 安卓可以用 IOS 报错。

    解决方案:https://www.qcloud.com/document/product/400/6973 ATS检测:https://www.qcloud.com/product/ssl#userD ...