Add a button to the top right of action bar

前端 未结 2 501
时光取名叫无心
时光取名叫无心 2021-02-02 10:30

Is there a way I can add a button to the top right of my ActionBar, like where the default settings Button is? I removed the settings Button

2条回答
  •  滥情空心
    2021-02-02 10:57

    You can add a button by editing/create the menu xml file:

    
    
    
        
    
    
    

    Then in your activity, if you created a new file your need to edit onCreateOptionsMenu

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
    

    and you can edit what the actions do in the following method:

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
    
        //noinspection SimplifiableIfStatement
        if (id == R.id.action_name) {
            return true;
        }
    
        return super.onOptionsItemSelected(item);
    }
    

提交回复
热议问题