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

问题描述 投票:0回答:1

我在android中创建了选项菜单。之后我必须将弹出菜单附加到选项菜单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;
        }
android popupmenu android-optionsmenu
1个回答
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);
}
© www.soinside.com 2019 - 2024. All rights reserved.