java MenuInflater 究竟是如何工作的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25179055/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 07:31:16  来源:igfitidea点击:

How exactly does the MenuInflater work?

javaandroid

提问by PenguinCake

If I get it right this far, MenuInflater is a type of object that can inflate (blow up, expand?) a Menu type object. But when is the method called and which Menu object is automatically passed in? What does getMenuInflater() do (or is it just another way of ... = new MenuInflater())? And then once the inflater object is created, what exactly does the .inflate do? What does the passed-in menu object do with my main.xml menu?

如果我到目前为止做对了,MenuInflater 是一种可以膨胀(爆炸,展开?)Menu 类型对象的对象。但是什么时候调用方法,自动传入哪个Menu对象呢?getMenuInflater() 有什么作用(或者它只是 ... = new MenuInflater() 的另一种方式)?然后一旦创建了 inflater 对象,.inflate 到底做了什么?传入的菜单对象对我的 main.xml 菜单有什么作用?

(I'm sorry if I'm asking too many questions at once.)

(如果我一次问太多问题,我很抱歉。)

public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main, menu);
    return super.onCreateOptionsMenu(Menu);
}

回答by Antoine Marques

A MenuInflateris an object that is able to create Menufrom xml resources (of course only menus resources), that is : construct a new instance of Menugiven a menu resource identifier.

AMenuInflater是一个能够Menu从 xml 资源(当然只有菜单资源)创建的对象,即:构造Menu给定菜单资源标识符的新实例。

The onCreateOptionMenu(Menu)is called when the menu button of the device is pressed, or either Activity.openOptionsMenu()is called.

onCreateOptionMenu(Menu)当按下设备的菜单按钮,或任一被称为Activity.openOptionsMenu()被调用。

The actual rendering of the menu is handled by the activity. Just before it is shown, the Activitypasses to you the menu so that you can fill it with your own items, then shows it.
So Android undertakes that since it's not your business to render the menu, you shall not control what menu is actually passed to you inside onCreateOptionsMenu.

菜单的实际呈现由活动处理。就在它显示之前,Activity菜单将传递给您,以便您可以用自己的项目填充它,然后显示它。
所以Android 承诺,由于渲染菜单不是您的事,您不应控制内部实际传递给您的菜单onCreateOptionsMenu



As for the ActionBar, the onCreateOptionsMenuis called when filling ActionBar, so that options from the menu be available in the ActionBar. This method is only called once after the Activitybe created. If you want to modify the menu later, you should instead override Activity.onPrepareOptionsMenu

至于ActionBaronCreateOptionsMenu则在填充时调用ActionBar,以便菜单中的选项在ActionBar. 该方法Activity在创建后只调用一次。如果你想稍后修改菜单,你应该覆盖Activity.onPrepareOptionsMenu

回答by Adrian

My understanding about "inflating" is "reading" a resource file (a XML file which describes a layout or GUI element) and "creating" the actual objects that correspond to it; this will make the menu object visible within an Android application. MenuInflaterclass represents an Android framework class specialized in parsing and constructing menus from menu resource files.

我对“膨胀”的理解是“读取”资源文件(描述布局或 GUI 元素的 XML 文件)并“创建”与其对应的实际对象;这将使菜单对象在 Android 应用程序中可见。 MenuInflater类表示专门用于从菜单资源文件解析和构建菜单的 Android 框架类。

Have a look at: Build Menus in Android with Java and XML: Introduction

看一看:使用 Java 和 XML 在 Android 中构建菜单:介绍

From the article:

从文章:

Once the menu resource file (my_menu.xml) is ready and has all the items you intend to add, you can use MenuInflater to override the function onCreateOptionsMenu. This function is supposed to fill the menu object with the desired menu items, but Android's MenuInflater class helps you create menu items from the XML resource mentioned above

一旦菜单资源文件 (my_menu.xml) 准备好并包含您要添加的所有项目,您就可以使用 MenuInflater 来覆盖 onCreateOptionsMenu 函数。此函数应该用所需的菜单项填充菜单对象,但 Android 的 MenuInflater 类可帮助您从上述 XML 资源创建菜单项