文章目录

  • LayoutInflater分析
    • 获取LayoutInflater对象
    • inflate方法参数说明
    • 源码分析
    • 案例说明
      • 当 root == null
      • 当 root != null && attachToRoot == true
      • 当 root != null && attachToRoot == false

LayoutInflater分析

获取LayoutInflater对象

LayoutInflater layoutInflater = LayoutInflater.from(context);
LayoutInflater layoutInflater = activity.getLayoutInflater();
//本质都是调用第三种方式
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

inflate方法参数说明

public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot)
resource root attachToRoot 说明
XML布局 root == null 失去作用 由于子View的宽高属性是相对于父View的,没有指定父View,所以导致他的宽高属性失效。
XML布局 root != null attachToRoot == true 会将子View添加到父View中
XML布局 root != null attacthToRoot == false 会将子View外层的layout属性进行设置,子View被添加到父View中时,这些layout属性会自动生效。

源码分析

public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot) {final Resources res = getContext().getResources();//1.使用反射尝试获取提前编译好的View对象//2.如果获取到View对象,则直接返回;如果没有获取到,则通过XML资源解析器View view = tryInflatePrecompiled(resource, res, root, attachToRoot);if (view != null) {return view;}//根据资源id获取XML布局解析器XmlResourceParser parser = res.getLayout(resource);try {//使用XML解析器对XML布局进行解析return inflate(parser, root, attachToRoot);} finally {parser.close();}
}
public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) {synchronized (mConstructorArgs) {final Context inflaterContext = mContext;final AttributeSet attrs = Xml.asAttributeSet(parser);Context lastContext = (Context) mConstructorArgs[0];mConstructorArgs[0] = inflaterContext;View result = root;try {advanceToRootNode(parser);final String name = parser.getName();//如果布局根标签为merge,则root不能为null,attachToRoot不能为false,否则抛出异常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");}//解析XML布局rInflate(parser, root, inflaterContext, attrs, false);} else {//根据XML解析获取根View对象final View temp = createViewFromTag(root, name, inflaterContext, attrs);ViewGroup.LayoutParams params = null;//如果root不为nullif (root != null) {//获取root的布局参数params = root.generateLayoutParams(attrs);if (!attachToRoot) {//如果attachToRoot==false,则View设置布局参数temp.setLayoutParams(params);}}//解析子布局rInflateChildren(parser, temp, attrs, true);//如果root!=null,且attachToRoot==true,则自动将布局添加到root中,并设置布局参数if (root != null && attachToRoot) {root.addView(temp, params);}//当root==null或者attachToRoot==false,则直接返回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(getParserStateDescription(inflaterContext, attrs)+ ": " + 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;}
}

案例说明

layout_btn

<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="200dp"android:layout_height="200dp"android:orientation="vertical"android:text="hello" />

当 root == null

View view = layoutInflater.inflate(R.layout.layout_btn, null);
ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
linearLayout.addView(view);

当 root != null && attachToRoot == true

View view = layoutInflater.inflate(R.layout.layout_btn, linearLayout, true);
ViewGroup.LayoutParams layoutParams = view.getLayoutParams();

当 root != null && attachToRoot == false

View view = layoutInflater.inflate(R.layout.layout_btn, linearLayout, false);
ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
linearLayout.addView(view);

Android LayoutInflater源码分析相关推荐

  1. Android HandlerThread 源码分析

    HandlerThread 简介: 我们知道Thread线程是一次性消费品,当Thread线程执行完一个耗时的任务之后,线程就会被自动销毁了.如果此时我们又有一 个耗时任务需要执行,我们不得不重新创建 ...

  2. Android ADB 源码分析(三)

    前言 之前分析的两篇文章 Android Adb 源码分析(一) 嵌入式Linux:Android root破解原理(二) 写完之后,都没有写到相关的实现代码,这篇文章写下ADB的通信流程的一些细节 ...

  3. 【Android SDM660源码分析】- 02 - UEFI XBL QcomChargerApp充电流程代码分析

    [Android SDM660源码分析]- 02 - UEFI XBL QcomChargerApp充电流程代码分析 一.加载 UEFI 默认应用程序 1.1 LaunchDefaultBDSApps ...

  4. 【Android SDM660源码分析】- 03 - UEFI XBL GraphicsOutput BMP图片显示流程

    [Android SDM660源码分析]- 03 - UEFI XBL GraphicsOutput BMP图片显示流程 1. GraphicsOutput.h 2. 显示驱动初化 DisplayDx ...

  5. 【Android SDM660源码分析】- 01 - 如何创建 UEFI XBL Protocol DXE_DRIVER 驱动及UEFI_APPLICATION 应用程序

    [Android SDM660源码分析]- 01 - 如何创建 UEFI XBL Protocol DXE_DRIVER 驱动及UEFI_APPLICATION 应用程序 一.创建DXE_DRIVER ...

  6. 【Android SDM660源码分析】- 04 - UEFI ABL LinuxLoader 代码分析

    [Android SDM660源码分析]- 04 - UEFI ABL LinuxLoader 代码分析 1. LinuxLoader.c 系列文章: <[Android SDM660开机流程] ...

  7. Android 音频源码分析——AndroidRecord录音(一)

    Android 音频源码分析--AndroidRecord录音(一) Android 音频源码分析--AndroidRecord录音(二) Android 音频源码分析--AndroidRecord音 ...

  8. Android框架源码分析——从设计模式角度看 Retrofit 核心源码

    Android框架源码分析--从设计模式角度看 Retrofit 核心源码 Retrofit中用到了许多常见的设计模式:代理模式.外观模式.构建者模式等.我们将从这三种设计模式入手,分析 Retrof ...

  9. 人人网官方Android客户端源码分析(1)

    ContentProvider是不同应用程序之间进行数据交换的标准API,ContentProvider以某种Uri的形式对外提供数据,允许其他应用访问或修改数据;其他应用程序使用ContentRes ...

  10. Android 音频源码分析——AudioTrack设备选择

    Android 音频源码分析--AndroidRecord录音(一) Android 音频源码分析--AndroidRecord录音(二) Android 音频源码分析--AndroidRecord音 ...

最新文章

  1. AliOS Things 3.0应用笔记:摄像头配网 + 钉钉群通知 + 天气显示
  2. js上拉加载ajax数据,原生ajax写的上拉加载实例
  3. mysql 日期函数大全_MYSQL 日期函数大全
  4. Debian, Ubuntu 和 Linux Mint 中安装WPS
  5. 【登陆设计】-【技术上】你会做WEB上的用户登录功能吗?
  6. Gradle下载手动安装
  7. PS4在Jetson nano下的配对使用,并用ROS接口来控制
  8. 内存CL-RCD-RP-RAS含义
  9. linux原生桌面,亲手打造自己的Linux桌面环境
  10. autojs打开微信扫一扫,扫描二维码等信息的脚本
  11. 禅道 10.0.alpha 版本发布,全新的界面和交互体验
  12. 【面经】2021 中国农业银行 笔试编程题
  13. SpringBoot定时任务说明
  14. Java代码审计--checklist
  15. Hyper-v搭建K8s v1.18.6 单主集群环境(包括dashboard)
  16. Spring Cloud 微服务及相关技术总结
  17. Linux 虚拟内存和物理内存的理解(转)
  18. vim列删除、列修改
  19. 墨卡托坐标以及 墨卡托坐标转经纬度
  20. python中0代表什么_python语言中,0.1**0.3表示什么_学小易找答案

热门文章

  1. STM32键盘扫描程序
  2. android会员管理,基于Android平台的会员管理系统设计与实现
  3. xz1刷Android10,索尼xz1国行版安卓9.0固件
  4. 解决Mac版 snipaste 不在菜单栏显示,无法修改快捷键
  5. 重磅白皮书发布,华为持续引领未来智慧园区建设新模式
  6. 别人的底鼓/808为什么比你有力?你可能忘了用这个插件
  7. ad怎么批量改元器件封装_ad中如何批量修改封装
  8. ViewPage的基本使用以及动画效果的添加
  9. mysql nlssort_Oracle数据库中文拼音,部首,笔画排序问题,NLS_SORT设置
  10. 系统学习深度学习(十六)--Overfeat