【问题标题】:Android Show DropDown Menu on MenuItem clickAndroid 在 MenuItem 点击时显示下拉菜单
【发布时间】:2017-07-04 07:37:02
【问题描述】:

我想在 MenuItem 上显示 DropDown 菜单点击就像this

这样

请注意,此项目是这样添加的:

<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() 已被弃用。

谢谢!

【问题讨论】:

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


    【解决方案1】:

    如下创建菜单 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);
         }
     }
    

    【讨论】:

    • 查看更新后的问题!我已经知道你提到的那件事了。
    • 这看起来像一个弹出菜单。
    • 谢谢!哟!它按我想要的方式工作。我完全忘记了我们也可以创建子菜单。还是谢谢。
    【解决方案2】:

    尝试自定义弹出菜单

    菜单.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  
             }  
    

    【讨论】:

      【解决方案3】:

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

       @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/

      【讨论】:

      • 有一种方法,但它不是最简单的......我看过一个可能有帮助的教程。我会谷歌它并告诉你我是否找到它
      • 看我贴的教程
      • 我查看了教程,但没有看到它在哪里提到这一点。如果您展示了 v 的声明,那就太好了;因为我不知道 v 是什么。它是一个菜单项吗?因为 menuItem 是弹出窗口应该锁定的内容。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-31
      • 1970-01-01
      • 2016-12-06
      • 1970-01-01
      相关资源
      最近更新 更多