Android中LayoutInflater.inflater()的正确打开方式
作者:MrTrying 发布时间:2022-04-12 16:25:57
前言
LayoutInflater在开发中使用频率很高,但是一直没有太知道LayoutInflater.from(context).inflate()
的真正用法,今天就看看源码的流程。
首先来看from()的源码:
/**
* 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;
}
其实就是从Context中获取Context.LAYOUT_INFLATER_SERVICE所对应的系统服务。这里涉及到Context实现以及服务创建的源码,不继续深究。
重点是通常所使用的inflate()
方法,比较常用的就是这两个:
inflate(@LayoutRes int resource, @Nullable ViewGroup root)
inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot)
另外两个方法inflate(XmlPullParser parser, @Nullable ViewGroup root)
和inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot)
,
而两个参数的方法,实际也是调用了三个参数的inflate()
方法,只是在三个参数传入了root!=null
public View inflate(@LayoutRes int resource, @Nullable ViewGroup root) {
return inflate(resource, root, root != null);
}
那我们就可以直接看三个参数的inflate()
方法了,其中res.getLayout(resource)
这句代码,已经将我们传入的layout布局的根布局的xml属性都加载到了XmlResourceParser中
public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot) {
final Resources res = getContext().getResources();
//省略代码
final XmlResourceParser parser = res.getLayout(resource);
try {
return inflate(parser, root, attachToRoot);
} finally {
parser.close();
}
}
这里其实就会发现,最后return调用的其实是inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot)
这个方法,所谓的四个inflate()
方法,其他三个只是对这个方法的重载,主要代码还是在这个方法中实现的
这部分代码较长,以注释的形式解释代码
public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) {
synchronized (mConstructorArgs) {
Trace.traceBegin(Trace.TRACE_TAG_VIEW, "inflate");
final Context inflaterContext = mContext;
//1.通过XmlResourceParser对象转换成AttributeSet
final AttributeSet attrs = Xml.asAttributeSet(parser);
Context lastContext = (Context) mConstructorArgs[0];
mConstructorArgs[0] = inflaterContext;
View result = root;
try {
//2.在xml中寻找根节点,如果类型是XmlPullParser.START_TAG或者XmlPullParser.END_DOCUMENT就会退出循环
int type;
while ((type = parser.next()) != XmlPullParser.START_TAG &&
type != XmlPullParser.END_DOCUMENT) {
// Empty
}
//3.如果根节点类型不是XmlPullParser.START_TAG将抛出异常
if (type != XmlPullParser.START_TAG) {
throw new InflateException(parser.getPositionDescription()
+ ": No start tag found!");
}
final String name = parser.getName();
//4.判断根节点是否是merge标签
if (TAG_MERGE.equals(name)) {
if (root == null || !attachToRoot) {
throw new InflateException("<merge /> can be used only with a valid "
+ "ViewGroup root and attachToRoot=true");
}
rInflate(parser, root, inflaterContext, attrs, false);
} else {
//5.通过根节点创建临时的view对象
final View temp = createViewFromTag(root, name, inflaterContext, attrs);
ViewGroup.LayoutParams params = null;
if (root != null) {
//6.如果root不为空,则调用generateLayoutParams(attrs)获取root所对应LayoutParams对象
params = root.generateLayoutParams(attrs);
//是否attachToRoot
if (!attachToRoot) {
//7.如果attachToRoot为false,则使用root默认的LayoutParams作为临时view对象的属性
temp.setLayoutParams(params);
}
}
//8.inflate xml的所有子节点
rInflateChildren(parser, temp, attrs, true);
//9.判断是否需要将创建的临时view attach到root中
if (root != null && attachToRoot) {
root.addView(temp, params);
}
//10.决定方法的返回值是root还是临时view
if (root == null || !attachToRoot) {
result = temp;
}
}
} catch (XmlPullParserException e) {
final InflateException ie = new InflateException(e.getMessage(), e);
ie.setStackTrace(EMPTY_STACK_TRACE);
throw ie;
} catch (Exception e) {
final InflateException ie = new InflateException(parser.getPositionDescription()
+ ": " + e.getMessage(), e);
ie.setStackTrace(EMPTY_STACK_TRACE);
throw ie;
} finally {
mConstructorArgs[0] = lastContext;
mConstructorArgs[1] = null;
Trace.traceEnd(Trace.TRACE_TAG_VIEW);
}
return result;
}
}
1中的XmlResourceParser在之前所获取的,包含了layout中跟布局的属性数据。
6,7则是很多时候使用inflate方法之后,发现xml布局设置的宽高属性不生效的部分原因,有时候在RecyclerView中添加就会这样。如果root!=null
且attachToRoot为false时,创建的view则会具有自身根节点属性值,与root对应的LayoutParam
9的判断决定了创建的view是否添加到root中,而10则决定了方法返回的是root还是view
总结
根据inflate的参数不同可以获得不同的返回值
root | attachToRoot | 返回值 |
---|---|---|
null | false(或者true) | 返回resource对应的view对象,但是xml中根节点的属性没有生效 |
!=null | false | 返回resource对应的view对象,并且xml中根节点的属性生效,view对象的LayoutParam与root的LayoutParam对应 |
!=null | true | 返回root对象,对应resource创建的view对象,xml中根节点的属性生效,并且将会添加到root中 |
注意:attachToRoot默认为root!=null
的值
来源:https://www.jianshu.com/p/b235b2516d4f


猜你喜欢
- 在android开发中我们常常遇到与到乱码问题,遇到乱码问题首先我们要先检查两端编码格式是否一致!一般我们提交数据用get 和post方法,
- 上一篇介绍了使用springmvc集成shiro登陆过程,通过FormAuthenticationFilter过滤器获取到用户输入的账号密码
- 下面一段代码给大家介绍了android 自定义顶部导航栏控件功能,具体代码如下所示:class HeaderBar @JvmOverload
- 在android提供了一种类型:Parcel。被用作封装数据的容器,封装后的数据可以通过Intent或IPC传递。 除了基本类型以外,只有实
- 现在Android上架各大平台都要求App首页添加一个弹框,显示用户协议以及一些隐私政策,不然上架各大平台,现在就来简单的实现一下这个对话框
- 之前在博文中为了更好的给大家演示APP的实现效果,本人了解学习了几种给手机录屏的方法,今天就给大家介绍两种我个人用的比较舒服的两种方法:(1
- bean.xml文件p标签使用报错The prefix "p" for attribute "p:某属性&qu
- 一、配置逆向generatoe.xml<?xml version="1.0" encoding="UTF
- gRPCgRPC是由 google开发的一个高性能、通用的开源RPC框架,主要面向移动应用开发且基于HTTP/2协议标准而设计,同时支持大多
- 本文实例讲述了Android在JNI中使用ByteBuffer的方法。分享给大家供大家参考。具体如下:一、ByteBuffer 定义在NIO
- 在 APK 开发中,通过 Java 代码来打开系统的安装程序以安装 APK 并不是什么难事,一般的 Android 系统都有开放这一功能。但
- 本文实例讲述了WinForm中实现picturebox自适应图片大小的方法。分享给大家供大家参考,具体如下:picturebox控件共有两种
- 首先给出代码和输出://import java.sql.DriverManager;//import java.sql.SQLExcepti
- 一、分步骤集成1.1 整合连接池hikariCP介绍:HikariCP 是一个高性能的 JDBC 连接池组件,可以避免连接频繁建立、关闭的开
- 这是一个演示如何使用java执行定时任务的实例,本实例开始运行后不会自动结束,请在运行本实例后手动结束程序。package com.hong
- 什么是继承面向对象的特征:封装:不必要公开的数据成员和方法,使用private关键字进行修饰。意义:安全性。背景代码中创建的类, 主要是为了
- 什么是树?简单认识树 在生活中,有杨树,石榴树,枣树,而在计算机中的树呢,是一种非线性结构,是由 n(n>=0) 个有限节点
- 目录开启定时任务注解@EnableScheduling@Scheduled添加定时任务Cron表达式在线cron工具适应场景springBo
- 1 使用Office自带的库前提是本机须安装office才能运行,且不同的office版本之间可能会有兼容问题,从Nuget下载 Micro
- 本文实例讲述了C#事件用法。分享给大家供大家参考。具体分析如下:EventHandler<TEventArgs>的定义如下pub