首页 文章

使用menu.addIntentOptions()时自定义菜单选项

提问于
浏览
0

我正在构建一个基于Google提供的Note pad应用程序的简单应用程序 . 我的第一步是将应用程序转换为尽可能使用XML菜单 . 在主要活动,笔记列表中,我使用MenuInflater显示默认的'Compose'菜单选项:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);

    // menu initialization, use the baseline menu from XML
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.noteslist, menu);

    // generate any additional actions that can be performed on the
    // overall list.  In a normal install, there are no additional
    // actions found here, but this allows other applications to extend
    // our menu with their own actions.
    Intent intent = new Intent(null, getIntent().getData());
    intent.addCategory(Intent.CATEGORY_ALTERNATIVE);
    menu.addIntentOptions(Menu.CATEGORY_ALTERNATIVE, 0, 0, new ComponentName(this, NotesList.class), null, intent, 0, null);

    return true;
}

使用noteslist.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu 
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/compose_note"
      android:icon="@drawable/ic_menu_compose"
      android:title="@string/menu_compose"
      android:alphabeticShortcut="c"
      android:numericShortcut="3" />
</menu>

一切正常 . 现在,根据示例(并在示例中修改时,如果列表中没有选择任何项目,则会尝试添加意图选项),如果列表中有项目,我想添加一些其他选项,并选择其中一个:

@Override
public boolean onPrepareOptionsMenu(Menu menu) {

    super.onPrepareOptionsMenu(menu);

    // determine if we have any items in the list via the ListAdapter
    final boolean haveItems = (getListAdapter().getCount() > 0);

    // do we have items?
    if (haveItems) {
        // there are items, check if any are selected
        //Toast.makeText(getApplicationContext(), "position: " + getSelectedItemPosition(), Toast.LENGTH_SHORT).show();
        if (getSelectedItemPosition() >= 0) {
            // an item is selected, add the intents for one of our list items to the menu
            Uri uri = ContentUris.withAppendedId(getIntent().getData(), getSelectedItemId());

            // build menu on the fly... always starts with the EDIT action
            Intent[] specifics = new Intent[1];
            specifics[0] = new Intent(Intent.ACTION_EDIT, uri);
            MenuItem[] items = new MenuItem[1];

            // now add additional CATEGORY_ALTERNATIVE intent-based actions, (see the manifest)
            Intent intent = new Intent(null, uri);
            intent.addCategory(Intent.CATEGORY_ALTERNATIVE);
            menu.addIntentOptions(Menu.CATEGORY_ALTERNATIVE, 0, 0, null, specifics, intent, 0, items);

            // finally, add a shortcut to the edit menu item
            if (items[0] != null) {
                items[0].setShortcut('1', 'e');
            }
        }
    } else {
        menu.removeGroup(Menu.CATEGORY_ALTERNATIVE);
    }

    return true;
}

最后,引用的AndroidManifest.xml与演示应用程序的默认值保持不变:

http://developer.android.com/resources/samples/NotePad/AndroidManifest.html

所以,我有两个问题:

1)选择项目时由onPrepareOptionsMenu()生成的结果菜单,编辑注释和编辑 Headers ,使用默认图标并且没有指定快捷方式 . 我可以通过android:icon =“”将intent-filter设置为具有不同的图标,但是没有分配字母和数字快捷方式的运气......我想指定这些,并希望可能有一种方法在XML中定义这些菜单项,当它们通过intent-filters识别时由app引入时,也会拉动XML并以某种方式膨胀/导入它 . 有什么建议或指示?

2)在onCreateOptionsMenu()中,为什么addIntentOptions()调用CATEGORY_ALTERNATIVE不会将具有intent-filter设置为category.ALTERNATIVE的活动添加到菜单中(在这种情况下不添加正确的行为,只是试图了解如何在onCreateOptionsMenu()和onPrepareOptionsMenu()中对addIntentOptions()进行几乎相同的调用会产生不同的菜单 .

1 回答

  • 0

    为了解决这个问题,我从上面的评论中得到了解决方案,这实际上是Android开发已经从使用Intents转移到由于菜单污染而生成菜单项,如此thread中所示 . Android团队:

    “......我们放弃了这种方法,因为管理任意数量的附加项目的UI是一个挑战......”

相关问题