弹出菜单在选项菜单android中不起作用

时间:2014-02-19 11:10:42

标签: android popupmenu android-optionsmenu

我在android.After中创建了选项菜单,我必须将弹出菜单附加到选项菜单icon.in弹出菜单中它需要锚视图但我有选项菜单id.So如何从选项菜单id.please帮助获取锚视图我

代码:

 switch (item.getItemId()) {
            case R.id.settings:

                PopupMenu popup = new PopupMenu(NavigationActivity.this,R.id.settings); ==>error 
                // Inflating the Popup using xml file
                popup.getMenuInflater().inflate(R.menu.main, popup.getMenu());

                return super.onOptionsItemSelected(item);
            }
            return true;
        }

1 个答案:

答案 0 :(得分:0)

使用样式单击选项菜单项上的弹出菜单

@Override
public boolean onOptionsItemSelected(MenuItem item) {

int id = item.getItemId();
AtomicBoolean selected = new AtomicBoolean(false);

if (id == ru.asv.dokiks.R.id.viewMenuFilter) {

  PopupMenu popupMenu =
    new PopupMenu(
      new ContextThemeWrapper(
        this,
        R.style.my_style),
      findViewById(
        ru.asv.dokiks.R.id.my_options_menu_item_view),
      Gravity.START);

  // Обработчики выбора:

  popupMenu.setOnMenuItemClickListener(
    (android.view.MenuItem item1) -> {

      if (
        item1
          .getTitle()
          .equals(
            getString(
              R.string.first_popup_menu_item))) {

        // React.
      }

      if (
        item1
          .getTitle()
          .equals(
            getString(
              R.string.second_popup_menu_item))) {

        // React.
      }

      selected.set(true);

      return true;
    });

  popupMenu.inflate(
    R.menu.my_popup_menu);

  popupMenu.show();

  selected.set(true);
}

return selected.get() || super.onOptionsItemSelected(item);
}