inflate(int resource, ViewGroup root, boolean attachToRoot)见解

本文详细解析了LayoutInflater的inflate方法,包括三个参数的具体含义与作用。通过实例说明了如何使用inflate方法加载布局资源,并解释了不同参数设置对布局加载的影响。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/**
* Inflate a new view hierarchy from the specified xml resource. Throws
* {@link InflateException} if there is an error.
*
* @param resource ID for an XML layout resource to load (e.g.,
* <code>R.layout.main_page</code>)
* @param root Optional view to be the parent of the generated hierarchy (if
* <em>attachToRoot</em> is true), or else simply an object that
* provides a set of LayoutParams values for root of the returned
* hierarchy (if <em>attachToRoot</em> is false.)
* @param attachToRoot Whether the inflated hierarchy should be attached to
* the root parameter? If false, root is only used to create the
* correct subclass of LayoutParams for the root view in the XML.
* @return The root View of the inflated hierarchy. If root was supplied and
* attachToRoot is true, this is root; otherwise it is the root of
* the inflated XML file.
*/

inflate(int resource, ViewGroup root, boolean attachToRoot)

view=inflater.inflate(R.layout.fragment1, null,false)的三个参数详解;

第一个参数:加载view的布局文件。

第二个参数:

1.参数为空的情况,xml里的根布局(也就是最外层布局),将不会被加载(xml文件的根布局会失效);

2.不为空的情况,

系统会调用params = root.generateLayoutParams(attrs);//根据xml文件的宽,高 获取params

 temp.setLayoutParams(params); 会把这个布局加载进去;也就是说xml文件的根布局不会失效;

第三个参数:

如果为true:

系统会调用:root.addView(temp, params); 会自动调用addview();

 

https://blog.youkuaiyun.com/dongpeng0314/article/details/51737899

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值