什么是LayoutInflater,如何正确使用它? [英] What is LayoutInflater and how do I use it properly?

查看:122
本文介绍了什么是LayoutInflater,如何正确使用它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Android中的LayoutInflater到底是什么?使用它的预期方法是什么?我可以找到不同类型的用法,但是找不到我所用的套件.

What exactly is a LayoutInflater in Android? What is the intended method to use it? I could find different types of usage but unable to find which one suites in my case.


关于问题

我对inflate()方法的正确用法有很多困惑.在互联网上进行调查时,大多数结果是错误的或不完整的.甚至官方文档也很模糊.这篇文章是我在不同地方可以找到的总结.我相信这将对像我这样的初学者有所帮助

I had so many confusions about the proper usage of the inflate() method. When researched on the internet, Most of the results was either wrong or incomplete. Even the official doc is very vague. This post is a sum up of what I could find in different places. I believe this will be helpful to beginners like me

推荐答案

什么是LayoutInflater?

LayoutInflater 是用于从创建视图的类href ="https://developer.android.com/guide/topics/resources/layout-resource" rel ="noreferrer">布局资源(xml)文件或

What is a LayoutInflater?

LayoutInflater is a class used to create views from a layout resource (xml) file or a node of it (XmlPullParser objects).

这些可以表示单个视图或视图层次结构.

These can be a representation of either a single view or a view hierarchy.


要膨胀视图,我们需要一个LayoutInflater对象.除了创建新对象外,我们通常使用这些方法之一来获取具有上下文的现有对象.

To inflate a view, we need a LayoutInflater object. Instead of creating new object, we use one of these methods normally to get an existing object with the context.

第一个是最简单的,因为它很简单.

The first one is most commonly used because of its simplicity.

这是最后两种方法的示例用法.

Here are sample usages of the last two methods.

LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE)
LayoutInflater inflater = (LayoutInflater)getSystemService(LayoutInflater.class)


要膨胀视图,可以使用LayoutInflater#inflate()方法.它具有以下四种形式.如果源是布局资源,则可以使用前两种方法之一.如果源是布局资源节点,则使用最后两种方法.

To inflate views, LayoutInflater#inflate() method can be used. It has four forms as listed below. One of the first two methods can be used if the source is a layout resource. Last two methods are used if the source is a layout resource node.

  1. View inflate(int resource, ViewGroup root)

View inflate(XmlPullParser parser, ViewGroup root)

根:是新创建的视图层次结构将要附加到的ViewGroup.

root: It is a ViewGroup to which the newly created view hierarchy is about to attached.

attachToRoot::第一种和第三种方法将新创建的视图层次结构创建后,将其附加到根.但是,如果选择通过ViewGroup#addView()手动添加它,或者应该在其他位置进行附加,则可以选择第二个或最后一个方法并将attachToRoot设置为false.
例如,在FragmentonCreateView()中,以及在创建为RecyclerViewitemView的视图时.您应该在这两个地方将attachToRoot设置为false,因为附加将在其他地方完成.如果将其设置为true或在此类位置使用第一种或第三种方法,则会引发错误.

attachToRoot: The first and third methods attaches the newly created view hierarchy to the root once it is created. However if you choose manually add it by ViewGroup#addView() or attaching should be taken place at somewhere else, then you can choose the second or last method and set attachToRoot as false.
For instance, inside Fragment’s onCreateView() and when creating a view as RecyclerView’s itemView. You should set attachToRoot as false in these two places because the attaching will be done somewhere else. If we set it as true or use the first or third method in such places, it will throw an error.

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.


如果attachToRoot为true,则结果将是根视图.否则它将是新创建的视图层次结构.

If attachToRoot is true, the result will be the root view. Otherwise it will be the newly created view hierarchy.

理论上,所有这些方法都返回相同的内容-根视图.但是,对我们来说,它们并不相同.是吗?

Theoretically all these methods returns the same thing - the root view. However, to us, they are not the same. Are they?


可以看出,即使知道root,也将root设置为null.如果attachToRootfalse,则根可以为空.但是,应尽可能给出它,因为它用于创建LayoutParams的正确子类.

It is seen that setting root as null even if it is known. Root can be null if attachToRoot is false. However, it should be given if possible because it is used to create the correct subclass of LayoutParams.

这篇关于什么是LayoutInflater,如何正确使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆