MenuItem上的Android Show DropDown菜单单击

时间:2015-04-20 08:16:56

标签: android drop-down-menu android-studio android-actionbar

我想在 MenuItem 上显示 DropDown 菜单,只需点击this

enter image description here

喜欢这个

enter image description here

请注意,此项目添加如下:

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

在我的代码中:

@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);
 }

我已经看到了link但我已经知道ActionBar.setListNavigationCallbacks()已被弃用。

谢谢!

3 个答案:

答案 0 :(得分:19)

按照以下

创建菜单xml
 <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>

然后

@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);
     }
 }

答案 1 :(得分:0)

单击该项目时显示弹出菜单怎么样? 这是代码:

 @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;
                        }
                    });

                }
            });
    }

这是medecin_list_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>

上次修改: 请参阅本教程http://www.androidhive.info/2013/11/android-working-with-action-bar/

答案 2 :(得分:0)

尝试自定义弹出菜单

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>  

在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  
         }