以编程方式打开(溢出)菜单

时间:2015-03-27 09:29:26

标签: android

我需要在点击主页按钮时以编程方式打开(溢出)菜单。

与其他主题一样,我尝试过Activity.openOptionsMenu()ActionBarActivity().getSupportActionBar().openOptionsMenu(),但两次都没有发生。

二手代码:

@Override
public boolean onOptionsItemSelected(final MenuItem item) {
  ...
     case android.R.id.home:
        Log.i("HOME", "clicked");

        this.openOptionsMenu();
        break;
  ...
}

4 个答案:

答案 0 :(得分:6)

如果您使用 Route::get('/', array('as'=>'adverts', 'uses'=>'advertsController@index')); Route::get('/{id}', array('as'=>'advert', 'uses'=>'advertsController@info')); {{link_to_route('advert', array('id' => $advert->id))}} 的新Toolbar课程,请执行此操作:

Support Library

答案 1 :(得分:0)

我在下面做的事情希望能帮助你: -

// R.id.over_flow_item this your id in which your task has been perform
toolbar.getMenu().perperformIdentifierAction(R.id.over_flow_item,0);

<强> perperformIdentifierAction

public abstract boolean performIdentifierAction (int id, int flags)
Added in API level 1
Execute the menu item action associated with the given menu identifier.
Parameters
id
Identifier associated with the menu item.
flags
Additional option flags or 0.
Returns
If the given identifier exists and is shown, returns true; else returns false.
See Also
FLAG_PERFORM_NO_CLOSE

答案 2 :(得分:0)

如果您没有ActionBar,我找到的唯一方法是模拟KeyEvent。这使选项菜单出现

    BaseInputConnection  mInputConnection = new BaseInputConnection( findViewById(R.id.main_content), true);
KeyEvent kd = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MENU);
KeyEvent ku = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MENU);
mInputConnection.sendKeyEvent(kd);
mInputConnection.sendKeyEvent(ku);

答案 3 :(得分:0)

如果您在应用中使用自定义工具栏,则可以使用以下方式,

 new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    toolbar.showOverflowMenu();
                }
            }, 500);