inflate用一个XML源填充view. LayoutInflater

本文深入探讨了Android中LayoutInflater类的使用方法,包括如何从XML布局文件加载视图对象,以及inflate方法的详细参数解析,帮助开发者高效地进行布局加载与视图生成。

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


java.lang.Object
    android.view.LayoutInflater
This class is used to instantiate layout XML file into its corresponding View objects.
 It is never be used directly -- 
 use getLayoutInflater()  or getSystemService(String)  to retrieve a standard LayoutInflater instance 
 that is already hooked up to the current context and correctly configured for the device you are running on. 
 For example:

LayoutInflater inflater = (LayoutInflater)context.getSystemService
      Context.LAYOUT_INFLATER_SERVICE);
常用方法有:
public View inflate (int resource, ViewGroup root)
Since: API Level 1

Inflate a new view hierarchy from the specified xml resource. Throws InflateException if there is an error.
Parameters
resource     ID for an XML layout resource to load (e.g., R.layout.main_page)
root     Optional view to be the parent of the generated hierarchy.
Returns

    * The root View of the inflated hierarchy. If root was supplied, this is the root View; otherwise it is the root of the inflated XML file. 
注意:如果root被提供的话,在把新生成的View连接到root后,返回root.否者返回的是新生成的View
public View inflate (int resource, ViewGroup root, boolean attachToRoot)
Since: API Level 1

Inflate a new view hierarchy from the specified xml resource. Throws InflateException if there is an error.
Parameters
resource     ID for an XML layout resource to load (e.g., R.layout.main_page)
root     Optional view to be the parent of the generated hierarchy (if attachToRoot is true), or else simply an object that provides a set of LayoutParams values for root of the returned hierarchy (if attachToRoot is false.)
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.
Returns

    * 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. 
注意:如果root被提供而且attachToRoot为TRUE的话,在把新生成的View连接到root,返回root.否者返回的是新生成的View。
如果root被提供但attachToRoot为FALSE的话,root只是把它的LayoutParams参数给新生成的View用,
不会把新生成的View连接到root。当然返回的是新生成的View。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值