Android - Popup menu when list item view pressed?

后端 未结 7 1567
逝去的感伤
逝去的感伤 2020-12-24 15:33

i would like to implement a popup menu similar to google\'s play store as shown below.

\"enter

7条回答
  •  爱一瞬间的悲伤
    2020-12-24 15:53

    Using popup menu it's quite simple to create a menu with these three steps:

    1 - Add a click listener to the menu button using OnClickListener or as i prefer from the layout xml:

    
    

    2 - Create the menu layout menu_layout.xml:

    
    
        
        
    
    

    3 - Create a popup menu, inflate the xml layout and show it:

    public void showMenu (View view)
    {
        PopupMenu menu = new PopupMenu (this, view);
        menu.setOnMenuItemClickListener (new PopupMenu.OnMenuItemClickListener ()
        {
            @Override
            public boolean onMenuItemClick (MenuItem item)
            {
                int id = item.getItemId();
                switch (id)
                {
                    case R.id.item_settings: Log.i (Tag, "settings"); break;
                    case R.id.item_about: Log.i (Tag, "about"); break;
                }
                return true;
            }
        });
        menu.inflate (R.menu.menu_layout);
        menu.show();
    }
    

提交回复
热议问题