How I can place overflow menu below toolbar instead of overflow menu to overlaps the app bar

前端 未结 2 1159
轻奢々
轻奢々 2020-12-15 12:56

I know that it is per material guideline, but i don\'t like it and i want it to below toolbar. please provide some info to adjust position of overflow menu.

2条回答
  •  天命终不由人
    2020-12-15 13:34

    With the above solution I had the problem that the menu item background was transparent and also It was kind of blocking every action on the window until I click the menu item. For the ones having my problem I suggest adding a popup menu to the menu item. For example, I have this item on my menu.xml:

    
    

    Then, on my OnOptionsItemsSelected method (after inflating my menu to the activity):

    @Override
    public boolean onOptionsItemSelected(MenuItem menuItem)
    {
        switch (menuItem.getItemId())
        {
            case android.R.id.home:
                finish();
                break;
    
            case R.id.new_work_report:
                View itemView = FieldSheetActivity.this.findViewById(R.id.new_work_report);
                PopupMenu popMenu = new PopupMenu(MyActivity.this, itemView);
                popMenu.getMenu().add("Do something");
    
                popMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener()
                {
    
                    @Override
                    public boolean onMenuItemClick(MenuItem item)
                    {
                        // Your desired action
    
                        return true;
                    }
                });
                popMenu.show();
                break;
        }
    
        return super.onOptionsItemSelected(menuItem);
    }
    

    With this solution, the menu options always are shown below the clicked menu item. In case you have any questions, just ask me!

提交回复
热议问题