工具栏中 Home 键的问题 [英] Problems with Home button in the Toolbar

查看:27
本文介绍了工具栏中 Home 键的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是一个常见问题,但我尝试了很多 StackOverflow 的解决方案,任何解决方案都有效.

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 back button or nothing.

我尝试了不同的代码,例如:

I tried different codes like:

    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);

现在我有了代码:

    setSupportActionBar(toolbar);
    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

它显示后退按钮.

显示主页按钮的任何解决方案?感谢您的帮助.

Any solution to show the Home button? Thanks for the help.

如果我的英语太差,我很抱歉.

I'm sorry if my English is too bad.

推荐答案

您应该使用 DrawerToggle 来管理 DrawerLayout 的状态.

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.

这是一个工作示例:

        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();

这篇关于工具栏中 Home 键的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆