Android Show DropDown Menu on MenuItem click

2020-03-05 02:32发布

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

enter image description here

Like this

enter image description here

Note that this item was added like:

<item
    android:id="@+id/menu_item_action_parameters"
    android:title="@string/text_parameters"
    android:icon="@drawable/ic_menu_parameter"
    app:showAsAction="ifRoom|withText"/>
</item>

And in my code:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    switch (id) {
        case R.id.menu_item_action_parameters:
            // What to do here?
            break;
    }
    return super.onOptionsItemSelected(item);
 }

I have seen this link but I have came to know that ActionBar.setListNavigationCallbacks() has been deprecated.

Thanks!

3条回答
家丑人穷心不美
2楼-- · 2020-03-05 03:15

Create your menu xml as follow

 <menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
       android:id="@+id/menu_item_action_parameters"
       android:title="@string/text_parameters"
       android:icon="@drawable/ic_menu_parameter"
       app:showAsAction="ifRoom|withText"/> >
       <menu>
          <item 
            android:id="@+id/action_dropdown1"
            android:title="@string/dropdown_1" />
          <item 
            android:id="@+id/action_dropdown2"
            android:title="@string/dropdown2" />
          <item 
            android:id="@+id/action_dropdown3"
            android:title="@string/dropdown3" />
        </menu>
    </item>

    <item
      more item
    </item>
</menu>

Then

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    switch (id) {
        case R.id.action_dropdown1:
            ...
            return true;

        case R.id.action_dropdown2:
            ...
            return true;
        ...

        default:
            return super.onOptionsItemSelected(item);
     }
 }
查看更多
【Aperson】
3楼-- · 2020-03-05 03:28

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)

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:id="@+id/Appeler"
        android:title="@string/Appeler" />
    <item
        android:id="@+id/EnvoyerMsg"
        android:title="@string/envoyerMsg" />
    <item
        android:id="@+id/Afficher"
        android:title="@string/Afficher" />
    <item
        android:id="@+id/AvoirRdv"
        android:title="@string/avoirRdv" />
    <item
        android:id="@+id/AfficherDet"
        android:title="@string/afficherDet" />

</menu>

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

查看更多
放我归山
4楼-- · 2020-03-05 03:32

try custom popup menu

menu.Xml

<menu xmlns:androclass="http://schemas.android.com/apk/res/android" >  

    <item  
        android:id="@+id/one"  
        android:title="One"/>  

    <item  
        android:id="@+id/two"  
        android:title="Two"/>  

    <item  
        android:id="@+id/three"  
        android:title="Three"/>  

</menu>  

call this code on buttonClick

 button = (Button) findViewById(R.id.button1);  
          button1.setOnClickListener(new OnClickListener() {  

           @Override  
           public void onClick(View v) {  
            //Creating the instance of PopupMenu  
            PopupMenu popup = new PopupMenu(MainActivity.this, button1);  
            //Inflating the Popup using xml file  
            popup.getMenuInflater().inflate(R.menu.menu, popup.getMenu());  

            //registering popup with OnMenuItemClickListener  
            popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {  
             public boolean onMenuItemClick(MenuItem item) {  
              Toast.makeText(MainActivity.this,"You Clicked : " + item.getTitle(),Toast.LENGTH_SHORT).show();  
              return true;  
             }  
            });  

            popup.show();//showing popup menu  
           }  
          });//closing the setOnClickListener method  
         }  
查看更多
登录 后发表回答