How to display menu item with icon and text in AppCompatActivity

前端 未结 9 1815
遥遥无期
遥遥无期 2020-11-29 05:11

I tried different combinations in xml file:



        
9条回答
  •  粉色の甜心
    2020-11-29 05:29

    You need to add tools:context="your class" to menu tag

    menu.xml

    
    
    
    
       
           
               
    
               
    
               
               
    
    
               
               
           
       
    
    
    
    

    Override onCreateOptionsMenu() Method

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu); 
        menu.getItem(0).getSubMenu().getItem(3).setVisible(false);
        menu.getItem(0).getSubMenu().getItem(4).setVisible(true);
        return super.onCreateOptionsMenu(menu);
    } 
    

    You should write tool:context to menu tag then run you will get icons before your text.

提交回复
热议问题