View.Inflate()和LayoutInflate填充布局详解

LayoutInflate布局加载原理详解 布局加载源码分析 书接上文,从View的绘制流程开始: Window、DecorView、ViewRootImp详解 View的绘制流程-measure、layout、draw 先从Activity.setContentView开始简单回顾下布局是如何被加载的 Activity.java public void setContentView(@LayoutRes int layoutResID) { getWindow().setContentView(layoutResID); initW 阅读详情

View.Inflate()和LayoutInflate填充布局详解

View.Inflate()

View.Inflate(Context context,int resource, ViewGroup root)

查看源码:

public static View inflate(Context context, @LayoutRes int resource, ViewGroup root) {
    LayoutInflater factory = LayoutInflater.from(context);
    return factory.inflate(resource, root);
}

可以看出其内部调用的 LayoutInflate 的两个参数方法

LayoutInflate填充布局

首先通过 LayoutInflater.from(this)获取 LayoutInflate 实例

LayoutInflater layoutInflater = LayoutInflater.from(this);

一般我们常用的调用方法如下:

layoutInflater.inflate(int resource, ViewGroup root);
layoutInflater.inflate(int resource, ViewGroup root, boolean attachToRoot);

1.两个参数的:layoutInflater.inflate(int resource, ViewGroup root);

    查看源码

public View inflate(@LayoutRes int resource, @Nullable ViewGroup root) {
       return inflate(resource, root, root != null);
     }

    其内部还是调用的三个参数的方法

2.三个参数的layoutInflater.inflate(int resource, ViewGroup root, boolean attachToRoot);
第一个参数:布局文件的id
第二个参数:根据第三个参数来决定是布局文件的父控件,还是只是为布局文件提供LayoutParams参数
第三个参数:是否依赖第二个控件成为其子控件

查看源码


layoutInflater.inflate(resource, null,false)

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();
        }
    }
public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) {
        synchronized (mConstructorArgs) {
            Trace.traceBegin(Trace.TRACE_TAG_VIEW, "inflate");

            final Context inflaterContext = mContext;
            final AttributeSet attrs = Xml.asAttributeSet(parser);
            Context lastContext = (Context) mConstructorArgs[0];
            mConstructorArgs[0] = inflaterContext;
            View result = root;

            try {
                // Look for the root node.
                int type;
                while ((type = parser.next()) != XmlPullParser.START_TAG &&
                        type != XmlPullParser.END_DOCUMENT) {
                    // Empty
                }

                if (type != XmlPullParser.START_TAG) {
                    throw new InflateException(parser.getPositionDescription()
                            + ": No start tag found!");
                }

                final String name = parser.getName();

                if (DEBUG) {
                    System.out.println("**************************");
                    System.out.println("Creating root view: "
                            + name);
                    System.out.println("**************************");
                }

                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 {
                    // Temp is the root view that was found in the xml
                    final View temp = createViewFromTag(root, name, inflaterContext, attrs);

                    ViewGroup.LayoutParams params = null;
                    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);
                        }
                    }

                    if (DEBUG) {
                        System.out.println("-----> start inflating children");
                    }

                    // Inflate all children under temp against its context.
                    rInflateChildren(parser, temp, attrs, true);

                    if (DEBUG) {
                        System.out.println("-----> done inflating children");
                    }

                    // 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);
                    }

                    // Decide whether to return the root that was passed in or the
                    // top view found in xml.
                    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 {
                // Don't retain static reference on context.
                mConstructorArgs[0] = lastContext;
                mConstructorArgs[1] = null;

                Trace.traceEnd(Trace.TRACE_TAG_VIEW);
            }

            return result;
        }
    }

当第二个参数为null的时候,第三个参数为 true 或者 false 已经意义不大,布局文件中根布局的 layout_width 和 layout_height 都已经失效

	..........
    final View temp = createViewFromTag(root, name, inflaterContext, attrs);

	..........
 	if (root == null || !attachToRoot) {
            result = temp;
        }
	..........
	return result;

可以看出只是返回了View,并没有给其设置 LayoutParams

layoutInflater.inflate(resource, root,false)

	..........
    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);
    	 }
     }
    	..........
    
    if (root == null || !attachToRoot) {
        result = temp;
    }
    	..........
    return result;

有源码可以看出第二个参数不为null,第三个参数为false,只是用root提供LayoutParams参数来获取,但是并没有给其指定父控件

layoutInflater.inflate(resource, root,true)

..........
 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);
         }
 }
..........
if (root != null && attachToRoot) {
    root.addView(temp, params);
}
..........
return result;

可以看出将 temp 按照 params 参数添加到 root 控件中

切记

在我们使用 ListView 和 RecyclerView 的时候,填充布局时不能写成layoutInflater.inflate(resource, root,true),其第三个参数不能为true,否则将会报错!

参考文章: View inflate方法和LayoutInflater inflate方法的区别详解

                Android LayoutInflater深度解析 给你带来全新的认识

【DRAM存储器二】Sense Amplifier 详述DRAM颗粒中感应放大器功能、电路结构、工作原理和操作波形 阅读详情

相关推荐

从零开始:如何在STM32上搭建QP框架实现事件驱动开发(附完整代码)

本文详细介绍了如何在STM32嵌入式平台上从零开始搭建QP框架,实现事件驱动开发。通过对比传统前后台系统与RTOS的局限,深入剖析了QP框架基于活动对象和层次式状态机的核心优势,并提供了完整的环境配置、代码移植及一个智能灯光控制器的实战案例,帮助开发者构建清晰、可维护的复杂嵌入式应用。

weixin_29200741的博客 487

View.inflate()和LayoutInflater.inflate()的区别

文章目录 一、LayoutInflater.inflate() 1. 获取LayoutInflater实例 2. LayoutInflater.inflate()的重载 二、 View.inflate() 三、总结 一、LayoutInflater.inflate() 该方法适用于所有布局填充的的场景,但使用前需要先实例化LayoutInflater对象 1. 获取LayoutInflater实例 getLayoutInflater(); 这个方法可以在..

az44yao的专栏 419

新手别怕!用嘉立创EDA从零画一块ESP8266最小系统板(附完整PCB源文件)

本文详细指导新手如何使用嘉立创EDA从零开始设计ESP8266最小系统板,涵盖原理图绘制、PCB布局、布线技巧及设计验证等关键步骤。通过实战案例和实用技巧,帮助读者快速掌握PCB设计基础,轻松打造自己的物联网开发板,并附完整PCB源文件供参考。

weixin_28716769的博客 619

开源全能命令行工具箱:聚合百种实用功能,提升开发者效率

在软件开发和系统运维领域,命令行工具是工程师日常工作的核心组件,它们遵循Unix哲学——每个工具专注于做好一件事。然而,随着工具数量的增长,碎片化管理、环境依赖和记忆负担成为效率瓶颈。聚合型命令行工具箱通过统一入口和一致接口,将系统信息查询、文件操作、网络测试、编码转换等高频功能集成于单一二进制文件,实现了开箱即用和跨平台支持。这种设计显著降低了工具发现与使用的认知成本,让开发者能快速响应端口扫描、批量重命名、数据格式转换等常见需求。通过整合热词如“端口扫描”和“批量重命名”,工具箱将零散操作标准化,成为终

weixin_30596735的博客 522

OnlyOffice Docker部署全攻略:解除连接限制+文件大小限制+中文字体优化

本文详细介绍了OnlyOffice企业级Docker部署的完整解决方案,重点破解连接限制、文件大小限制和中文字体优化三大核心痛点。通过定制化镜像部署、性能调优配置和中文字体深度优化,帮助企业实现高效稳定的文档协作平台。

weixin_28837817的博客 368

Unity3D 2022版如何一键安装Newtonsoft.Json?最新Package Manager教程

本文详细介绍了在Unity3D 2022及以上版本中,如何通过Package Manager一键安装和配置Newtonsoft.Json(Json.NET)。教程涵盖了从打开Package Manager、搜索并安装正确包源,到验证安装和基础使用的完整流程,旨在帮助开发者以现代化、可靠的方式集成这一强大的JSON处理框架,提升开发效率并规避手动导入的常见问题。

lg888的博客 921

三种inflate方式的区别

在安卓开发中,经常要将一个xml布局文件inflate成一个view对象,用到的方法通常是三种: (1)View.inflate(Context context, int resource, ViewGroup root); (2)LayoutInflater.from(context).inflate(int resource, ViewGroup root); (3)getSystemServ...

whoami_I的博客 829

uniCloud云存储进阶:七牛云扩展存储从开通、绑定域名到批量上传的完整实战

本文详细介绍了uniCloud云存储进阶实践,重点讲解如何通过七牛云扩展存储实现从开通、绑定域名到批量上传的完整流程。内容涵盖成本优势分析、域名备案方案、SSL证书配置、多文件并发上传控制等实战技巧,并提供可直接复用的代码示例,帮助开发者高效构建内容社区类应用。

weixin_26985923的博客 468

Android --- View.inflate()的详细介绍

误用 LayoutInflater 的 inflate() 方法已经不是什么稀罕事儿了…… 做 Android 开发做久了,一定会或多或少地对布局的渲染有一些懵逼: 1.View.inflate() 和 LayoutInflator.from().inflate() 有啥区别? 2.调用 inflate() 方法的时候有时候传 null,有时候传 parent 是为啥? 3.用 LayoutInf...

梁同学与Android 1万+

std::map看来可以被淘汰了?可以用vector来替代,而且更好

vector的优势在于内存连续,可以先std::sort,然后std::lower_bound,或者std::binary_search。而std::map,insert的时候,需要new出内存。

tumu_C的博客 1019

Allegro快速编辑丝印文字操作指导

选择setup-Application Mode-placement edit。Allegro支持丝印文字的编辑,下面介绍快速编辑丝印文字的两种方法如下。这个功能还支持拷贝粘贴,可以从其它地方拷贝过来,免去了书写的麻烦。出现text edit窗口,输入自己需要的文字,点击ok。鼠标移动到丝印文字位置,文字会被高亮起来。Allegro快速编辑丝印文字操作指导。点击丝印文字,丝印会被高亮起来。选择Text edit 命令。输入需要更改后的文字,如下。右击选择text edit。以编辑下方丝印文字为例。

weixin_54787054的博客 4192

View.inflate() 和LayoutInflater.inflate()的区别

前言: 既然在平淡的一天,也要坚持给自己找点乐趣。 在android开发中,根据布局文件生成View的情况咋们开发中是十分常见的,现在咋们分析下这这两个方法的区别 LayoutInflater.inflate() ? public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolea...

xueyoubangbang的博客 1096

车载网络测试实操源码_使用CAPL解析VBF文件

VBF(Versatile Binary Format)文件是一种二进制文件格式,被沃尔沃、福特、马自达、吉利等汽车制造商广泛采用。这种格式无法使用我们常规的烧录器进行刷写,通常需要进行解析转化。如果各位需要自己编写软件或脚本来刷写VBF文件,或者需要对刷写功能进行测试时,那么本篇文章就会对你非常有用。今天我们就来讲一下如何使用CAPL脚本对VBF文件进行数据解析。VBF文件包含三部分:VBF版本段、文件头段、数据段。前面的版本段和文件头段是ASCII码,包含版本、地址、校验码等信息

文章主要涉及嵌入式软件开发、测试、项目管理等,欢迎交流,共同进步。 2996

关于LayoutInflate和View-infalte的参数意义以及区别总结

关于LayoutInflate和View.infalte的参数意义以及区别总结

王小六的博客 2049

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

LayoutInflate(布局填充器)的使用

1.初始化   ● 通过Activity的方法 `LayoutInflater layoutInflater=mActivity.getLayoutInflater();`    ● 通过LayoutInflate的静态方法 `LayoutInflater layoutInflater=LayoutInflater.from(mContext);`    ● 通过Context的方法

weixin_38123155的博客 703

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

View inflate方法和LayoutInflater inflate方法的区别详解

我们在Android开发中,对于将布局填充成View对象,最常用的两种办法是:View类的方法inflate和LayoutInflater类的inflate方法,今天有朋友问到这两种填充方法的区别,就查看了一下两者的区别,写成文章,以方便有需要的人。 首先我们要清楚两者大致的区别,之后我们再来慢慢看两者具体的不同之处。 LayoutInflater类的inflate方法适用于所有需要进行布局填充的...

boguesfei的博客 219

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

android布局优化过程,Android布局优化(一)LayoutInflate — 从布局加载原理说起

如需转载请评论或简信,并注明出处,未经允许不得转载系列文章目录前言最近打算写一些Android布局优化相关的文章,既然要进行布局优化,就要先了解布局加载原理,才能知道有哪些地方可以作为优化的切入点。开发同学做任何事情最好都能够知其所以然布局加载源码分析我们先从Activity.setContentView开始分析布局是如何被加载的Activity.setContentView(@LayoutRes...

weixin_39600447的博客 379

LayoutInflater的inflate函数用法详解

LayoutInflater作用是将layout的xml布局文件实例化为View类对象。 获取LayoutInflater的方法有如下三种: ? LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

zhll3377的专栏 972

LayoutInflate用法和解析

//两个参数 public View inflate(@LayoutRes int resource, @Nullable ViewGroup root) { return inflate(resource, root, root != null); } //三个参数 public View inflate (int resource, ViewGroup root, boolean attachToRoot) 首先我先说下我对每个参数的理解 第一个参数:想要添加的布局 第二个参数:想要添

Sour_orange的博客 317

android inflater 参数,LayoutInflater.inflate()方法两个参数和三个参数

转载请标明出处:https://www.cnblogs.com/tangzh/p/7074853.html很多人都用过layoutinflater(布局填充器)对于我来说通常使用下面两种:layoutinflater.from(context).inflate(r.layout.recycle_foot_item,null);layoutinflater.from(context).inflate...

weixin_30755903的博客 280

View绘制之LayoutInflate源码分析

获取实例 第一种方法 /** * Obtains the LayoutInflater from the given context. */ public static LayoutInflater from(Context context) { LayoutInflater LayoutInflater = (LayoutInflater) context.getSystemService

哆啦U梦的博客 734

LayoutInflate动态加载view

1.若我们采用view = inflater.inflate(R.layout.item_list,null);方式填充视图,item布局中的根视图的layout_XX属性会被忽略掉,然后设置成默认的wrap_content 2.如果我们想保证item的视图中的参数不被改变,我们需要使用view = inflater.inflate(R.layout.item_list, parent,fals

lhcxwjh的专栏 705

LayoutInflater和inflate()方法的用法

LayoutInflater作用及使用   (2011-04-01 17:19:03) 转载▼ 标签:  it 分类: android世界 作用:  1、对于一个没有被载入或者想要动态载入的界面, 都需要使用inflate来载入.  2、对于一个已经载入的Activity, 就可以使用实现了这个Activiyt的的fin

逃出丛林 389

关于LayoutInflate的理解。

首先,LayoutInflater这个类是用来干嘛的呢? 我们最常用的便是LayoutInflater的inflate方法,这个方法重载了四种调用方式,分别为: 1. public View inflate(int resource, ViewGroup root) 2. public View inflate(int resource, ViewGroup root, bool

puhaibo_skynet的专栏 682

LayoutInflater.inflate()方法两个参数和三个参数

转载请标明出处:https://www.cnblogs.com/tangZH/p/7074853.html很多人都用过LayoutInflater(布局填充器)对于我来说通常使用下面两种:LayoutInflater.from(context).inflate(R.layout.recycle_foot_item,null);LayoutInflater.from(context).inflat...

weixin_34066347的博客 146

Android中LayoutInflater的使用,inflate

LayoutInflater的使用,在实际开发种LayoutInflater这个类还是非常有用的,它的作用类于  findViewById(),不同点是LayoutInflater是用来找layout下xml布局文件,并且实例化!而findViewById()是找具体xml下的具体 widget控件(如:Button,TextView等)。       通俗的说,inflate就相当于将一

tasty 658
上一篇: Android中View的setClickable,setEnable,setFocusable,setFocusableInTouchMode区别
下一篇: TextView中文排版问题
StarkSongV
博客等级 码龄10年 9粉丝 14原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值