How to add overflow menu to Toolbar?

前端 未结 3 820
粉色の甜心
粉色の甜心 2020-12-09 16:09

I\'m trying to use Android ActionBar in my app, and have an option that\'s hidden away in the overflow menu.

There\'s a lot of documentation out there,

3条回答
  •  萌比男神i
    2020-12-09 16:53

    in manifest file declare

    android:theme="@style/AppTheme.NoActionBar"
    

    like this :

    
    

    and add this to your style :

     
    

    and call this in Activity onCreate() :

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    

    override this method in activity:

     @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.product_list, menu);
        //U can find item set icon and stuff...
        MenuItem item= menu.findItem(R.id.action_search);
    
        return true;
    }
    

    and declare your menu like this for overflow menu:

      
    
        
            
            
        
    
    

    and for handle item selection call this

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        switch (id) { 
           case R.id.sign_out:
           //do stuff
               break;
               }  
            return super.onOptionsItemSelected(item);
        }
    

    done :)

提交回复
热议问题