Problems with Home button in the Toolbar

前端 未结 1 1179
臣服心动
臣服心动 2020-12-21 15:53

I know it\'s a common question but I tried a lot of solutions of StackOverflow and any solution works.

I want to show the home button in the toolbar but it shows the

相关标签:
1条回答
  • 2020-12-21 16:41

    You should use a DrawerToggle to manage the state of the DrawerLayout.

    Using a custom icon is really not a good way to achieve this, you should use Material Colors to change the color of the toggle button.

    This is a working example of this :

            mToolbar = (Toolbar) findViewById(R.id.toolbar);
            mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    
            setSupportActionBar(mToolbar);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setHomeButtonEnabled(true);
    
            mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, mToolbar,  R.string.drawer_open, R.string.drawer_close) {
    
                /** Called when a drawer has settled in a completely closed state. */
                public void onDrawerClosed(View view) {
                    super.onDrawerClosed(view);
    
                    invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
                }
    
                /** Called when a drawer has settled in a completely open state. */
                public void onDrawerOpened(View drawerView) {
                    super.onDrawerOpened(drawerView);
    
                    invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
                }
            };
    
    
            // Set the drawer toggle as the DrawerListener
            mDrawerLayout.setDrawerListener(mDrawerToggle);
            mDrawerToggle.syncState();
    
    0 讨论(0)
提交回复
热议问题