how to set toolbar on FragmentActivity?

后端 未结 4 1653
日久生厌
日久生厌 2021-02-19 06:52

I want to set toolbar on my activity which extends FragmentActivity. I know that for use setSuppoertActionBar(toolbar) method we e

4条回答
  •  暖寄归人
    2021-02-19 07:36

    You should extend your Activity from AppCompatActivity as this includes support for Fragments and the Toolbar. And then override the

    onCreateOptionsMenu() and onOptionsItemSelected() methods

    You shouldn't be overriding

    onMenuItemSelected()

    public class MainActivity extends AppCompatActivity { ...
    
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        FragmentTransaction ft = getSupportFragmentManager.beginTransaction();
        ....
    
    
        ....
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
          ...
             ...
        }
    
    
       @Override
       public boolean onOptionsItemSelected(MenuItem item) {
    
       ...
      }
    

提交回复
热议问题