简体   繁体   English

MenuInflater究竟是如何工作的?

[英]How exactly does the MenuInflater work?

If I get it right this far, MenuInflater is a type of object that can inflate (blow up, expand?) a Menu type object. 如果我到目前为止,MenuInflater是一种可以膨胀(炸毁,扩展?)菜单类型对象的对象。 But when is the method called and which Menu object is automatically passed in? 但是何时调用该方法以及哪个Menu对象自动传入? What does getMenuInflater() do (or is it just another way of ... = new MenuInflater())? getMenuInflater()做什么(或者它只是... =新的MenuInflater()的另一种方式)? And then once the inflater object is created, what exactly does the .inflate do? 然后,一旦创建了inflater对象,.inflate究竟做了什么? What does the passed-in menu object do with my main.xml menu? 传入的菜单对象对我的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);
}

A MenuInflater is an object that is able to create Menu from xml resources (of course only menus resources), that is : construct a new instance of Menu given a menu resource identifier. MenuInflater是一个能够从xml资源(当然只有菜单资源)创建Menu的对象,即:在给定菜单资源标识符的情况下构造Menu的新实例。

The onCreateOptionMenu(Menu) is called when the menu button of the device is pressed, or either Activity.openOptionsMenu() is called. 按下设备的菜单按钮或调用Activity.openOptionsMenu()时,将调用onCreateOptionMenu(Menu)

The actual rendering of the menu is handled by the activity. 菜单的实际呈现由活动处理。 Just before it is shown, the Activity passes to you the menu so that you can fill it with your own items, then shows it. 在它显示之前, Activity会向您传递菜单,以便您可以使用自己的项目填充它,然后显示它。
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 . 所以Android承诺,因为渲染菜单不是你的业务,你不应该控制在onCreateOptionsMenu实际传递给你的菜单。


As for the ActionBar , the onCreateOptionsMenu is called when filling ActionBar , so that options from the menu be available in the ActionBar . 对于ActionBar ,在填充ActionBar时会调用onCreateOptionsMenu ,因此菜单中的选项可在ActionBar This method is only called once after the Activity be created. 仅在创建Activity后调用此方法一次。 If you want to modify the menu later, you should instead override Activity.onPrepareOptionsMenu 如果您想稍后修改菜单,则应改为覆盖Activity.onPrepareOptionsMenu

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; 我对“膨胀”的理解是“读取”资源文件(描述布局或GUI元素的XML文件)和“创建”与其对应的实际对象; this will make the menu object visible within an Android application. 这将使菜单对象在Android应用程序中可见。 MenuInflater class represents an Android framework class specialized in parsing and constructing menus from menu resource files. 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. 一旦菜单资源文件(my_menu.xml)准备好并且包含您要添加的所有项目,就可以使用MenuInflater覆盖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 该函数应该用所需的菜单项填充菜单对象,但Android的MenuInflater类可以帮助您从上面提到的XML资源创建菜单项

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM