Android 中的 getLayoutInflator()
我已经阅读了有关 getLayoutInflator 的 android 文档,但我仍然不明白它的作用。有人可以给我这个方法的用例吗?或者您想在什么时间调用 getLayoutInflator?
I have read the android documentation about getLayoutInflator and I am still not understanding what it does. Can someone give me a use case for this method or may be during what time would you want to call getLayoutInflator?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Android 中的 XML 布局在使用之前需要进行扩充(解析为 View 对象)。
getLayoutInflator()
为您获取 LayoutInflator 的实例,该实例允许您手动扩展特定用途的布局。一个示例是在自定义
ArrayAdapter
中使用自定义布局填充ListView
。您需要在
ArrayAdapter
的重写getView()
方法中为每个单独的列表项手动填充并填充所需的布局。XML Layouts in Android need to be Inflated (parsed into View objects) before they are used.
getLayoutInflator()
gets you an instance of the LayoutInflator that will allow you to manually inflate layouts for specific uses.One example being in a Custom
ArrayAdapter
to populate aListView
with a Custom Layout.You need to manually inflate and populate your desired Layout for each individual list item in the
ArrayAdapter
's overriddengetView()
method.当您处于
Activity
中时,请使用setContentView()
。该方法会扩展布局并将所选布局显示为该活动的视图。但是,当您不在 Activity 中并且需要使用布局文件时,您必须对其进行扩充才能访问 XML 中的视图对象。Use
setContentView()
when you're in anActivity
. That method inflates the layout and displays the selected layout as the view for that Activity. But when you're NOT in an Activity and you need to work with a layout file, you have to inflate it to get access to the view objects in the XML.