Android:如何在另一个菜单xml中包含一个菜单xml? [英] Android: How to include a menu xml inside another menu xml?

查看:87
本文介绍了Android:如何在另一个菜单xml中包含一个菜单xml?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简单的问题。

我有我的子项菜单:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/fp_pitcher"
        android:title="Pitcher">
    </item>
    <item
        android:id="@+id/fp_catcher"
        android:title="Catcher">
    </item>
<!-- SNIP ---> 
</menu>

后来我想将其作为此菜单的子菜单包括:

And later I would want to include it as a submenu of this menu:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >


    <item
        android:id="@+id/teameditor_remove"
        android:title="Remove Player from Team">
    </item>

    <item
        android:id="@+id/teameditor_assignbattingposition"
        android:title="Assign Batting Position">
    </item>

    <item
        android:id="@+id/teameditor_assignfieldingposition"
        android:title="Assign Feilding Position">
        <!-- I want to include the submenu here-->
    </item>

</menu>

这里的问题回答了这个问题-我不确定如何给子菜单充气。

The question here kind of answered this - I'm not sure how to inflate the submenu.

我想您可以通过onContextItemSelected方法对其进行膨胀-但是膨胀需要一个菜单​​对象,该菜单对象不会传递给onContextItemSelected。

I'm thinking that you inflate it in the onContextItemSelected method - but inflate requires a menu object, which isn't passed into onContextItemSelected.

推荐答案

可悲的是,在纯XML中是不可能的,但是有一种不错的方法而不使用手动 Menu.add * 方法:以下是获取 Menu 实例以将其他文件包含/添加到其中的方法:

It's sadly not possible in plain XML, but there's a nice way without using manual Menu.add* methods: here's how you can obtain a Menu instance to include/inflate the other file into:

inflater.inflate(R.menu.player, menu);
MenuItem fp_menu = menu.findItem(R.id.teameditor_assignfieldingposition);
inflater.inflate(R.menu.positions, fp_menu.getSubMenu()); // needs <menu />

您可以使用指定的充气机将上面的代码放入以下任何内容


  • Activity.onCreateContextMenu(menu,v,menuInfo) getMenuInflater()

  • Fragment.onCreateContextMenu(menu,v,menuInfo) getActivity()。getMenuInflater()

  • Activity.onCreateOptionsMenu(menu) getMenuInflater()

  • Fragment.onCreateOptionsMenu(menu,inflater)充气机

  • Activity.onCreateContextMenu(menu, v, menuInfo): getMenuInflater()
  • Fragment.onCreateContextMenu(menu, v, menuInfo): getActivity().getMenuInflater()
  • Activity.onCreateOptionsMenu(menu): getMenuInflater()
  • Fragment.onCreateOptionsMenu(menu, inflater): inflater
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/teameditor_remove"
          android:title="Remove Player from Team"
    />
    <item android:id="@+id/teameditor_assignbattingposition"
          android:title="Assign Batting Position"
    />
    <item android:id="@+id/teameditor_assignfieldingposition"
          android:title="Assign Feilding Position">
        <menu><!-- include: positions.xml --></menu>
    </item>
</menu>

空的<菜单/> 占位符非常重要 ,否则 getSubMenu()将为 null

The empty <menu /> placeholder is very important, without that getSubMenu() will be null!

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/fp_pitcher"
          android:title="Pitcher"
    />
    <item android:id="@+id/fp_catcher"
          android:title="Catcher"
    />
    <!-- SNIP ---> 
</menu>



关于 onContextItemSelected 想法的注释



Note on your onContextItemSelected idea


我认为您可以通过 onContextItemSelected 方法将其充气[...]

I'm thinking that you inflate it in the onContextItemSelected method [...]

我认为如果您在 onContextItemSelected 中为时已晚,因为您重新处理了该事件,该事件将导致显示您的子菜单...尚未膨胀。您可以在 getSubMenu()中尝试同样的膨胀,但是我不确定它是否会出现。最好在应该创建菜单的地方创建菜单。

I think it's too late if you're in onContextItemSelected, since you're already handling the event which would lead to showing you're submenu... which is not inflated yet. You could try the same inflate into getSubMenu(), but I'm not sure that it'll show up. It's best to create the menu where it's supposed to be created.

未经测试 如果需要将相同的 positions.xml 充入 teameditor_assignbattingposition 以及 onOptionsItemSelected / onContextItemSelected 也会遇到一些问题。
的一种解决方法是将 findItem 变量转换为一个字段,然后保存对这两个变量的引用

Untested If you need to inflate the same positions.xml into teameditor_assignbattingposition as well you'll have some problems in onOptionsItemSelected/onContextItemSelected. One way to work around it is to convert the findItem variable to a field and save the reference to both

this.fp_menu = menu.findItem(R.id.teameditor_assignfieldingposition);
inflater.inflate(R.menu.positions, fp_menu.getSubMenu());
this.bp_menu = menu.findItem(R.id.teameditor_assignbattingposition);
inflater.inflate(R.menu.positions, bp_menu.getSubMenu());

然后在 on * ItemSelected 中:

switch (item.getItemId()) {
    case R.id.fp_pitcher:
        if (item == fp_menu.findItem(R.id.fp_pitcher)) {
            // selected inside teameditor_assignfieldingposition
        } else if (item == bp_menu.findItem(R.id.fp_picther)) {
            // selected inside teameditor_assignbattingposition
        } else {
            throw new ImLostInMenusException();
        }
        return true;
}
return super.on*ItemSelected();

这篇关于Android:如何在另一个菜单xml中包含一个菜单xml?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆