如何以编程方式创建一个菜单实例?即膨胀onCreateOptionsMenu外的菜单菜单、创建一个、实例、方式

2023-09-05 05:43:13 作者:溺死在皇冠海

我想夸大菜单对象的 onCreateOptionsMenu 方法(即创建/显示菜单时,用户没有$ P外$ PSS的按钮),所以我需要创建一个菜单实例,将它传递给充气的方法。

I want to inflate a menu object outside onCreateOptionsMenu method (which means to create/show the menu when the user doesn't press the button), so I need to create a menu instance to pass it to the inflate method.

下面是什么,我想实现的一个例子:

Here is an example of what I am trying to achieve:

Menu menu = // How to create an instance !? 
new MenuInflater(context).inflate(R.menu.my_menu, menu)

菜单是一个接口,所以我需要知道哪些类实现它。我做浏览的Andr​​oid code,以获取有关如何菜单对象创建的任何提示,但还是没找到我所期待的。

Menu is an interface, so I need to know which class is implementing it. I did browse Android code to get any hint on how a Menu object is created, but still could not find what I am looking for.

修改1

我的目标是火从一个自定义视图,这将是由活动处理的 onOptionsItemSelected(菜单项项目)事件,所以我需要有一个菜单项对象具体的itemId和标题与事件通过。

My goal is to fire an onOptionsItemSelected(MenuItem item) event from a custom view, which will be handled by the activity, so I need to have a MenuItem object with specific itemId and title to pass it with the event.

如果我可以成功地创建一个菜单对象,这将是很容易得到其子的MenuItems。

If I can successfully create a Menu object, it will be easy to get its children MenuItems.

编辑2

我的不可以试图显示菜单可言,我要的是填充一个ListView 的用元素的菜单中的XML定义的有标题,图标和的itemId,每当一个ListViewItem的被点击我要为火 onOptionsItemSelected(菜单项项目)事件这是我的活动处理

I am not trying to display a menu at all, what I want is to populate a ListView with elements defined in a menu XML that have title, icon and itemId and whenever a ListViewItem is clicked I want to fire a onOptionsItemSelected(MenuItem item) event that is handled in my activity.

我知道,我可以分析菜单的XML来提取物品的信息,但是我不能开火 onOptionsItemSelected(菜单项项目),而无需创建的标准的菜单项对象,把它作为参数。

I know that I can parse the menu XML to extract items information, however I will not be able to fire onOptionsItemSelected(MenuItem item) without creating a standard MenuItem object to pass it as argument.

任何帮助将AP preciated。谢谢!

Any help will be appreciated. Thanks!

推荐答案

我发现了两个解决方案,以编程方式创建一个菜单实例,它充气:

I found two solutions to programmatically create a Menu instance and inflate it:

使用 ActionbarSherlock 库或的 AppCompat V7库 菜单菜单=新的使用MenuBuilder(上下文); ,也可以编写自己的使用MenuBuilder

Using ActionbarSherlock library or AppCompat v7 library Menu menu = new MenuBuilder(context); or you can write your own MenuBuilder class

使用标准的Andr​​oid SDK:

Using standard android SDK:

//创建通过反射实例

// Creating an instance by reflection

Menu menu = newMenuInstance(context);


protected Menu newMenuInstance(Context context) {
    try {
        Class<?> menuBuilderClass = Class.forName("com.android.internal.view.menu.MenuBuilder");

        Constructor<?> constructor = menuBuilderClass.getDeclaredConstructor(Context.class);

        return (Menu) constructor.newInstance(context);

    } catch (Exception e) {e.printStackTrace();}

    return null;
}

一旦你有一个菜单实例,你可以很容易地从一个菜单XML资源的任何地方在你的程序膨胀

新MenuInflater(上下文).inflate(菜单Id,菜单);

我测试了这两种方法,他们都工作的很好,我会建议使用第二种方法与标准菜单菜单项的类Android SDK中的即使你的活动来延长SherlockActivity ,因为它仍然会收到 onOptionsItemSelected(菜单项项目)不管你用 android.view.MenuItem 或 com.actionbarsherlock.view.MenuItem

I tested both methods and they are working perfectly, I would recommend using the second method with the standard Menu and MenuItem classes from android SDK even if your activity extends SherlockActivity because it will still receive onOptionsItemSelected(MenuItem item) regardless if you fire it with android.view.MenuItem or com.actionbarsherlock.view.MenuItem

 
精彩推荐