how to set toolbar on FragmentActivity?

后端 未结 4 1642
日久生厌
日久生厌 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:26

    This thing is good when you are using NavigationDrawer use this:-

     toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
            getSupportActionBar().setDisplayShowTitleEnabled(false);
    

    then set Toolbar Title according to different fragment with different Titles in onNavigationItemSelected :-

    @Override
        public boolean onNavigationItemSelected(MenuItem item) {
            // Handle navigation view item clicks here.
    
            sfm = getSupportFragmentManager();
            Fragment fragment = new Fragment();
            int id = item.getItemId();
    
            if (id == R.id.nav_id) {
                fragment = new YourFragment();
                toolbar.setTitle("SET TOOLBAR NAME");
            }else if (id == R.id.nav_id2) {
                fragment = new YourFragment();
                toolbar.setTitle("SET TOOLBAR NAME");
            } 
    

    For single fragment, first customize your style.xml like this :-

    
    

    then apply into your custom toolbar:-

    
    
      // ...........
    
    
    
    

提交回复
热议问题