Android Show DropDown Menu on MenuItem click

前端 未结 3 841
故里飘歌
故里飘歌 2021-01-05 04:35

I want to show DropDown menu on MenuItem click just like this.

\"enter

3条回答
  •  [愿得一人]
    2021-01-05 04:55

    what about showing popup menu when clicking onthat item ? here is the code :

     @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();
    
    
    
            if (id == R.id.action_notifi) {
            // here we show the popup menu
            popup();
            }
    
            return super.onOptionsItemSelected(item);
        }
    
    
        public void popup(){
              PopupMenu popup = new PopupMenu(MainActivity.context, v); //the v is the view that you click replace it with your menuitem like : menu.getItem(1)
                        popup.getMenuInflater().inflate(R.menu.medecin_list_menu,
                                popup.getMenu());
                        popup.show();
                        popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                            @Override
                            public boolean onMenuItemClick(MenuItem item2) {
    
                                switch (item2.getItemId()) {
                                    case R.id.Appeler:
                                     //do somehting
    
                                        break;
                                    case R.id.EnvoyerMsg:
                                     //do somehting
    
                                        break;
                                    case R.id.AfficherDet:
                                //do somehting
    
                                        break;
                                    case R.id.Afficher: 
                            //do somehting
                                        break;
                                    case R.id.AvoirRdv:
                                  //do somehting
                                        break;
    
                                    default:
                                        break;
                                }
    
                                return true;
                            }
                        });
    
                    }
                });
        }
    

    and here is the medecin_list_menu (my menu)

    
    
        
        
        
        
        
    
    
    

    Last Edit: see this tutorial http://www.androidhive.info/2013/11/android-working-with-action-bar/

提交回复
热议问题