LayoutInflater.inflate()方法两个参数和三个参数
转载请标明出处:https://www.cnblogs.com/tangZH/p/7074853.html
更多精美文章:http://77blogs.com/?p=489 很多人都用过LayoutInflater(布局填充器) 对于我来说通常使用下面两种:
LayoutInflater.from(context).inflate(R.layout.recycle_foot_item,null);
LayoutInflater.from(context).inflate(R.layout.recycle_foot_item,parent,false);
那么两个参数与三个参数究竟有什么区别呢? 我们进去源码看一下两个参数时的代码:
public View inflate(@LayoutRes int resource, @Nullable ViewGroup root) {
return inflate(resource, root, root != null);
}
可以看出来使用两个参数时,它的内部也是调用了3个参数的方法。
如果我们使用LayoutInflater.from(context).inflate(R.layout.recycle_foot_item,null);
则实际上是调用了LayoutInflater.from(context).inflate(R.layout.recycle_foot_item,null,null!=null);
等同于:LayoutInflater.from(context).inflate(R.layout.recycle_foot_item,null,false);
如果我们使用LayoutInflater.from(context).inflate(R.layout.recycle_foot_item,parent);
则实际上是调用了LayoutInflater.from(context).inflate(R.layout.recycle_foot_item,parent,parent!=null);
等同于:LayoutInflater.from(context).inflate(R.layout.recycle_foot_item,parent,true);
我们再来看看三个参数的方法的源码:
public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot) {
final Resources res = getContext().getResources();
if (DEBUG) {
Log.d(TAG, "INFLATING from resource: \"" + res.getResourceName(resource) + "\" ("
+ Integer.toHexString(resource) + ")");
}
final XmlResourceParser parser = res.getLayout(resource);
try {
return inflate(parser, root, attachToRoot);
} finally {
parser.close();
}
}
在里面调用了
inflate(parser, root, attachToRoot);方法,在源码中可以看到,inflate(parser, root, attachToRoot);方法中有下面代码:
if (root != null) {
if (DEBUG) {
System.out.println("Creating params from root: " +
root);
}
//Create layout params that match root, if supplied
params = root.generateLayoutParams(attrs);
if (!attachToRoot) {
// Set the layout params for temp if we are not
// attaching. (If we are, we use addView, below)
temp.setLayoutParams(params);
}
}
.
.
.
// We are supposed to attach all the views we found (int temp)
// to root. Do that now.
if (root != null && attachToRoot) {
root.addView(temp, params);
}
从上面可以看出:
如果第二个和第三个参数均不为空的话,即root不为null,而attachToRoot为true的话那么我们执行完LayoutInflater.from(context).inflate(R.layout.recycle_foot_item,parent,true);之后,R.layout.recycle_foot_item就已经被添加进去parent中了,我们不能再次调用parent.add(View view)这个方法,否则会抛出异常。
那么root不为null,attachToRoot为false是代表什么呢?我们可以肯定的说attachToRoot为false,那么我们不将第一个参数的view添加到root中,那么root有什么作用?其实root决定了我们的设置给第一个参数view的布局的根节点的layout_width和layout_height属性是否有效。我们在开发的过程中给控件所指定的layout_width和layout_height到底是什么意思?该属性的表示一个控件在容器中的大小,就是说这个控件必须在容器中,这个属性才有意义,否则无意义。所以如果我们不给第一个参数的view指定一个父布局,那么该view的根节点的宽高属性就失效了。
如果我想让第一个参数view的根节点有效,又不想让其处于某一个容器中,那我就可以设置root不为null,而attachToRoot为false。这样,指定root的目的也就很明确了,即root会协助第一个参数view的根节点生成布局参数,只有这一个作用。
但是这个时候我们要手动地把view添加进来。
LayoutInflater.inflate()方法两个参数和三个参数的更多相关文章
- 三个案例带你看懂LayoutInflater中inflate方法两个参数和三个参数的区别
关于inflate参数问题,我想很多人多多少少都了解一点,网上也有很多关于这方面介绍的文章,但是枯燥的理论或者翻译让很多小伙伴看完之后还是一脸懵逼,so,我今天想通过三个案例来让小伙伴彻底的搞清楚这个 ...
- 【转】三个案例带你看懂LayoutInflater中inflate方法两个参数和三个参数的区别
关于inflate参数问题,我想很多人多多少少都了解一点,网上也有很多关于这方面介绍的文章,但是枯燥的理论或者翻译让很多小伙伴看完之后还是一脸懵逼,so,我今天想通过三个案例来让小伙伴彻底的搞清楚这个 ...
- View inflate方法和LayoutInflater inflate方法的区别详解
原创文章,转载请注明出处:http://www.cnblogs.com/baipengzhan/p/6257510.html 我们在Android开发中,对于将布局填充成View对象,最常用的两种办法 ...
- Android LayoutInflater.inflate(int resource, ViewGroup root, boolean attachToRoot)的参数理解
方法inflate(int resource, ViewGroup root, boolean attachToRoot) 中 第一个参数传入布局的资源ID,生成fragment视图,第二个参数是视图 ...
- Android编程之LayoutInflater的inflate方法详解
LayoutInflater的inflate方法,在fragment的onCreateView方法中经常用到: public View onCreateView(LayoutInflater infl ...
- 带你看懂LayoutInflater中inflate方法
关于inflate问题,我想很多人多多少少都了解一点,网上也有很多关于这方面介绍的文章,但是枯燥的理论或者翻译让很多小伙伴看完之后还是一脸懵逼,so,我今天想通过三个案例来让小伙伴彻底的搞清楚这个东东 ...
- android LayoutInflater.inflate()的参数介绍
LayoutInflater.inflate()的作用就是将一个xml定义的布局文件实例化为view控件对象: 与findViewById区别: LayoutInflater.inflate是加载一个 ...
- Android编程之LayoutInflater的inflate方法实例
假设你不关心其内部实现,仅仅看怎样使用的话,直接看这篇就可以. 接上篇,接下来,就用最最简单的样例来说明一下: 用两个布局文件main 和 test: 当中,main.xml文件为: <?xml ...
- Android LayoutInflater.inflate使用上的问题解惑
最近在在使用LayoutInflater.inflate方法时遇到了一些问题,以前没有仔细看过此类的使用方法,故将其记录下来,方便日后查阅. 相信大家都知道LayoutInflater.inflate ...
随机推荐
- You need to use a Theme.AppCompat theme (or descendant) with this activity 问题解决
You need to use a Theme.AppCompat theme (or descendant) with this activity 问题解决 问题代码 void initCommit ...
- [Swift]LeetCode893. 特殊等价字符串组 | Groups of Special-Equivalent Strings
You are given an array A of strings. Two strings S and T are special-equivalent if after any number ...
- 浮点型和BigDecimal的使用
//import java.math.*; package cn.xiaomu_01_rumrnjieduan; import java.math.BigDecimal; /** * 浮点型和BigD ...
- Jedis与Luttuce区别
如果你在网上搜索Redis 的Java客户端,你会发现,大多数文献介绍的都是 Jedis. 不可否认,Jedis是一个优秀的基于Java语言的Redis客户端. 但是,其不足也很明显:Jedis在实现 ...
- tensorflow 1.0 学习:用别人训练好的模型来进行图像分类
谷歌在大型图像数据库ImageNet上训练好了一个Inception-v3模型,这个模型我们可以直接用来进来图像分类. 下载地址:https://storage.googleapis.com/down ...
- solr之环境配置一
安装Java JDK solr运行需要java serverlet 容器,默认使用jetty,或者tomcat,jboss等等. 下载一个jdk,我的jdk是jdk1.7.0_65. 安装JDK的步骤 ...
- CentOS 7 安装 JAVA环境(JDK 1.8)
1.打开url选择jdk1.8下载 http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.htm ...
- Python爬虫入门教程 27-100 微医挂号网专家团队数据抓取pyspider
1. 微医挂号网专家团队数据----写在前面 今天尝试使用一个新的爬虫库进行数据的爬取,这个库叫做pyspider,国人开发的,当然支持一下. github地址: https://github.com ...
- JavaScript面向对象--封装
一.封装的概念 面向对象的类包括两大成员,一种是暴露给外部的接口,另一种是只在类内部才能访问的私有属性.在这个类被实例化成对象后,用户只能通过操作给定的接口来访问该类内部的私有属性,这就被称为面向对象 ...
- 基于open62541的opc ua 服务器开发实现(1)
关于opcua的介绍这里就不多说了,相信大家大都有了一些了解,open62541是一个开源C(C99)的opc-ua实现,开源代码可在官网或github上下载. 话不多说,首先搭建一个opcua服务器 ...