Android 中的 LayoutInflater 是做什么的?
Android 中 LayoutInflater
有什么用?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
Android 中 LayoutInflater
有什么用?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(15)
Inflater 实际上以某种方式转换为数据、视图、实例、可见的 UI 表示......因此它以编程方式利用来自适配器等的数据馈送。然后将其与您定义的 xml 集成,该 xml 告诉它数据应如何在 UI 中表示
Inflater actually some sort of convert to data, views, instances, to visible UI representation.. ..thus it make use of data feed into from maybe adapters, etc. programmatically. then integrating it with an xml you defined, that tells it how the data should be represented in UI
LayoutInflater 类用于实例化布局的内容XML 文件转换为相应的 View 对象。
换句话说,它采用 XML 文件作为输入并从中构建 View 对象。
The LayoutInflater class is used to instantiate the contents of layout XML files into their corresponding View objects.
In other words, it takes an XML file as input and builds the View objects from it.
LayoutInflator
是做什么的?当我第一次开始Android编程时,我对
LayoutInflater
和findViewById
感到非常困惑。有时我们使用其中一种,有时使用另一种。LayoutInflater
用于从您的 xml 布局之一创建新的View
(或Layout
)对象。findViewById
只是为您提供对已创建视图的引用。您可能认为您还没有创建任何视图,但是每当您在onCreate
中调用setContentView
时,Activity 的布局及其子视图就会在幕后膨胀(创建) 。因此,如果视图已经存在,则使用findViewById。如果没有,则使用 LayoutInflater 创建它。
示例
这是我制作的一个小型项目,显示了
LayoutInflater
和findViewById
的实际情况。没有特殊的代码,布局看起来像这样。蓝色方块是使用
include
插入到主布局中的自定义布局(请参阅此处 了解更多)。它会自动膨胀,因为它是内容视图的一部分。正如您所看到的,代码没有什么特别的。现在,让我们膨胀(创建)自定义布局的另一个副本并将其添加到其中。
为了膨胀新的视图布局,我所做的就是告诉膨胀器我的 xml 文件的名称 (
my_layout< /code>),我想要将其添加到的父布局 (
mainLayout
),但实际上我还不想添加它 (false
)。 (我也可以将父级设置为null
,但是这样我的自定义布局的根视图的布局参数将被忽略。)这里它再次处于上下文中。
请注意如何仅在布局已膨胀后使用
findViewById
。补充代码
这是上面示例的 xml。
activity_main.xml
my_layout.xml
什么时候需要 LayoutInflater
RecyclerView
中。 (请参阅这些RecyclerView
示例,了解 列表或网格。)您必须为每个可见项目膨胀一个新布局在列表或网格中。What does
LayoutInflator
do?When I first started Android programming, I was really confused by
LayoutInflater
andfindViewById
. Sometimes we used one and sometimes the other.LayoutInflater
is used to create a newView
(orLayout
) object from one of your xml layouts.findViewById
just gives you a reference to a view than has already been created. You might think that you haven't created any views yet, but whenever you callsetContentView
inonCreate
, the activity's layout along with its subviews gets inflated (created) behind the scenes.So if the view already exists, then use
findViewById
. If not, then create it with aLayoutInflater
.Example
Here is a mini project I made that shows both
LayoutInflater
andfindViewById
in action. With no special code, the layout looks like this.The blue square is a custom layout inserted into the main layout with
include
(see here for more). It was inflated automatically because it is part of the content view. As you can see, there is nothing special about the code.Now let's inflate (create) another copy of our custom layout and add it in.
To inflate the new view layout, all I did was tell the inflater the name of my xml file (
my_layout
), the parent layout that I want to add it to (mainLayout
), and that I don't actually want to add it yet (false
). (I could also set the parent tonull
, but then the layout parameters of my custom layout's root view would be ignored.)Here it is again in context.
Notice how
findViewById
is used only after a layout has already been inflated.Supplemental Code
Here is the xml for the example above.
activity_main.xml
my_layout.xml
When do you need LayoutInflater
RecyclerView
. (See theseRecyclerView
examples for a list or a grid.) You have to inflate a new layout for every single visible item in the list or grid.当您在
ListView
中使用自定义视图时,您必须定义行布局。您创建一个 xml,在其中放置 android 小部件,然后在适配器的代码中,您必须执行以下操作:
在 官方文档。
When you use a custom view in a
ListView
you must define the row layout.You create an xml where you place android widgets and then in the adapter's code you have to do something like this:
Read more in the official documentation.
LayoutInflater.inflate() 提供了一种将定义视图的 res/layout/*.xml 文件转换为可在应用程序源代码中使用的实际 View 对象的方法。
基本的两个步骤:获取充气器,然后充气资源
如何获得充气器?
假设 xml 文件是“list_item.xml”,如何获取视图?
LayoutInflater.inflate() provides a means to convert a res/layout/*.xml file defining a view into an actual View object usable in your application source code.
basic two steps: get the inflater and then inflate the resource
How do you get the inflater?
How do you get the view assuming the xml file is "list_item.xml"?
这是与前一个类似的另一个示例,但经过扩展以进一步演示它可以提供的膨胀参数和动态行为。
假设您的 ListView 行布局可以具有可变数量的 TextView。因此,首先您要膨胀基本项 View(就像前面的示例一样),然后在运行时循环动态添加 TextView。使用 android:layout_weight 另外可以完美地对齐所有内容。
以下是布局资源:
list_layout.xml
schedule_layout.xml
重写 BaseAdapter 类扩展中的 getView 方法
Note 不同膨胀方法调用:
Here is another example similar to the previous one, but extended to further demonstrate inflate parameters and dynamic behavior it can provide.
Suppose your ListView row layout can have variable number of TextViews. So first you inflate the base item View (just like the previous example), and then loop dynamically adding TextViews at run-time. Using android:layout_weight additionally aligns everything perfectly.
Here are the Layouts resources:
list_layout.xml
schedule_layout.xml
Override getView method in extension of BaseAdapter class
Note different inflate method calls:
该类用于将布局 XML 文件实例化为其相应的
View
对象。它永远不会直接使用 - 使用getLayoutInflater()
或getSystemService(String)
来检索已连接到的标准LayoutInflater
实例当前上下文并为您正在运行的设备正确配置。例如:参考:http://developer.android.com/reference/android/查看/LayoutInflater.html
This class is used to instantiate layout XML file into its corresponding
View
objects. It is never be used directly -- usegetLayoutInflater()
orgetSystemService(String)
to retrieve a standardLayoutInflater
instance that is already hooked up to the current context and correctly configured for the device you are running on. For example:Reference: http://developer.android.com/reference/android/view/LayoutInflater.html
LayoutInflater 是一个用于实例化布局 XML 的类文件转换为相应的视图对象,可以在Java程序中使用。
简单来说,在android中创建UI有两种方法。一种是静态方式,另一种是动态或编程方式。
假设我们有一个简单的布局 main.xml,其中包含一个
textview
和一个edittext
,如下所示。我们可以通过静态方式显示此布局。
创建视图的动态方式意味着该视图未在 main.xml 中提及,但我们希望在运行时显示。例如,布局文件夹中有另一个 XML 作为 footer.xml
我们希望在运行时在主 UI 中显示此文本框。所以在这里我们将膨胀text.xml。查看操作方法:
这里我使用 getSystemService (String) 来检索 LayoutInflater 实例。我也可以使用 getLayoutInflator() 来膨胀,而不是使用 getSystemService (String) ,如下所示:
LayoutInflater is a class used to instantiate layout XML file into its corresponding view objects which can be used in Java programs.
In simple terms, there are two ways to create UI in android. One is a static way and another is dynamic or programmatically.
Suppose we have a simple layout main.xml having one
textview
and oneedittext
as follows.We can display this layout in static way by
A dynamic way of creating a view means the view is not mentioned in our main.xml but we want to show with this in run time. For example, we have another XML in layout folder as footer.xml
We want to show this textbox in run time within our main UI. So here we will inflate text.xml. See how:
Here I have used getSystemService (String) to retrieve a LayoutInflater instance. I can use getLayoutInflator() too to inflate instead of using getSystemService (String) like below:
膨胀意味着读取描述布局(或 GUI 元素)的 XML 文件并创建与其对应的实际对象,从而使该对象在 Android 应用程序中可见。
该文件可以另存为date_time_dialog.xml:
此文件可以另存为 date_time_picker.xml:
MainActivity
类另存为 MainActivity.java:Inflating means reading the XML file that describes a layout (or GUI element) and to create the actual objects that correspond to it, and thus make the object visible within an Android app.
This file could saved as date_time_dialog.xml:
This file could saved as date_time_picker.xml:
The
MainActivity
class saved as MainActivity.java:inflater 的作用
它采用 xml 布局作为输入(比如说)并将其转换为 View 对象。
为什么需要
让我们考虑一个需要创建自定义列表视图的场景。现在每一行都应该是自定义的。但我们怎样才能做到呢。不可能将 xml 布局分配给 listview 的一行。因此,我们创建一个 View 对象。因此,我们可以访问其中的元素(textview、imageview 等),并将对象分配为 listview 的行。
因此,每当我们需要在某处分配视图类型对象并且我们有自定义 xml 设计时,我们只需通过 inflater 将其转换为对象,使用它。
What inflater does
It takes a xml layout as input (say) and converts it to View object.
Why needed
Let us think a scenario where we need to create a custom listview. Now each row should be custom. But how can we do it. Its not possible to assign a xml layout to a row of listview. So, we create a View object. Thus we can access the elements in it (textview,imageview etc) and also assign the object as row of listview
So, whenever we need to assign view type object somewhere and we have our custom xml design we just convert it to object by inflater and use it.
这是获取布局根视图引用的示例,
对其进行充气并与 setContentView(View view) 一起使用
here is an example for geting a refrence for the root View of a layout ,
inflating it and using it with setContentView(View view)
Layout inflater 是一个读取 xml 外观描述并将其转换为基于 java 的 View 对象的类。
Layout inflater is a class that reads the xml appearance description and convert them into java based View objects.
LayoutInflater 根据 XML 中定义的布局创建 View 对象。使用 LayoutInflater 有几种不同的方法,包括创建自定义视图、将 Fragment 视图膨胀到 Activity 视图、创建对话框,或者简单地将布局文件视图膨胀到 Activity 中。
关于通货膨胀过程如何运作存在很多误解。我认为这是由于 inflate() 方法的文档匮乏造成的。如果您想详细了解 inflate() 方法,我在这里写了一篇关于它的博客文章:
https://www.bignerdranch.com/blog/understanding-androids-layoutinflater-inflate/
LayoutInflater creates View objects based on layouts defined in XML. There are several different ways to use LayoutInflater, including creating custom Views, inflating Fragment views into Activity views, creating Dialogs, or simply inflating a layout file View into an Activity.
There are a lot of misconceptions about how the inflation process works. I think this comes from poor of the documentation for the inflate() method. If you want to learn about the inflate() method in detail, I wrote a blog post about it here:
https://www.bignerdranch.com/blog/understanding-androids-layoutinflater-inflate/
我的自定义列表希望它能说明概念
my customize list hope it illustrate concept
LayoutInflater 是 Android 中的一个基本组件。您必须一直使用它来将 xml 文件转换为视图层次结构。
LayoutInflater is a fundamental component in Android. You must use it all the time to turn xml files into view hierarchies.