How to force use of overflow menu on devices with menu button

前端 未结 11 672
谎友^
谎友^ 2020-11-22 14:04

I\'d like to have all of the menu items that don\'t fit into the ActionBar go into the overflow menu (the one that is reached from the Action Bar not the menu button) ev

11条回答
  •  无人共我
    2020-11-22 14:15

    I use to workaround it by defining my menu like this (also with ActionBarSherlock icon used in my example):

    
    
        
            
                
                
            
        
    
    
    

    I admit that this may require manual "overflow-management" in your xml, but I found this solution useful.

    You can also force device to use HW button to open the overflow menu, in your activity:

    private Menu mainMenu;
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // TODO: init menu here...
        // then:
        mainMenu=menu;
        return true;
    }
    
    @Override
    public boolean onKeyUp(int keycode, KeyEvent e) {
        switch(keycode) {
            case KeyEvent.KEYCODE_MENU:
                if (mainMenu !=null) {
                    mainMenu.performIdentifierAction(R.id.menu_overflow, 0);
                }
        }
    
        return super.onKeyUp(keycode, e);
    }
    

    :-)

提交回复
热议问题