如何从Android中的选项按钮中删除溢出菜单

时间:2015-12-09 10:01:11

标签: android

我试图使用新的可绘制源创建自定义溢出按钮启动活动而不是显示下拉菜单。我已经读过某个地方,我必须确保onOptionItemSelected中只有一个项目,所以这里是我的代码:

public boolean onOptionsItemSelected(MenuItem item) {
    // Inflate the menu; this adds items to the action bar if it is present.
    switch(item.getItemId()){
        case R.id.logout_button:
            Intent i = new Intent(PetVacActivity.this, LoginActivity.class);
            i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

            SharedPreferences sharedpreferences = getSharedPreferences(LoginActivity.MyPREFERENCES, Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedpreferences.edit();
            editor.clear();
            editor.commit();

            startActivity(i);
            finish();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }

}

但不幸的是,我的应用仍显示溢出按钮,但只有一个空白的下拉菜单,如此

enter image description here 而不是直接开始我的活动,我必须先点击下拉菜单来激活我的意图

5 个答案:

答案 0 :(得分:3)

在menu.xml中你可以添加

android:showAsAction="" 

带有这些值

never,ifRoom,always,withText,collapseActionView 

你可以在|中使用它们组合

在你的情况下你可以使用

android:showAsAction="always"

答案 1 :(得分:0)

就像这样调用工具栏的hideoverflowMenu函数。

toolbar.hideOverflowMenu();

答案 2 :(得分:0)

您可以使用子菜单和自己的图标

>> gpuArray.ones(1 + double(intmax('int32')), 1, 'uint8');
Error using gpuArray.ones
Maximum variable size allowed on the device is exceeded. 

答案 3 :(得分:0)

您可以尝试以下两种方法之一:

  1. 使用xml文件中定义的菜单
  2. 名为' my_menu'的菜单xml文件(使用appcompat库):

    <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto">
        <item android:id="@+id/log_out_menu_item"
              android:icon="@drawable/ic_log_out"
              android:title=""
              app:showAsAction="ifRoom"/>
    </menu>
    

    活动代码:

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.my_menu, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId()){
            case R.id.log_out_menu_item:
                // start new activity here
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
    
    1. 按代码添加菜单项
    2. 活动代码:

      private static final int LOG_OUT_MENU_ITEM_ID = 1;
      
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          MenuItem menuItem = menu.add(0, LOG_OUT_MENU_ITEM_ID, 0, "");
          menuItem.setIcon(R.drawable.ic_launcher_actionbar);
          MenuItemCompat.setShowAsAction(menuItem, MenuItemCompat.SHOW_AS_ACTION_IF_ROOM);
          return true;
      }
      
      public boolean onOptionsItemSelected(MenuItem item) {
          // Inflate the menu; this adds items to the action bar if it is present.
          switch(item.getItemId()){
              case LOG_OUT_MENU_ITEM_ID:
                  // start new activity here
                  return true;
              default:
                  return super.onOptionsItemSelected(item);
          }
      }
      

答案 4 :(得分:0)

终于搞定了!我只需要改变&#34; android:showAsAction&#34; to&#34; app:showAsAction&#34;。 感谢您的关注:))