在工具栏主页按钮的问题 [英] Problems with Home button in the Toolbar

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

问题描述

我知道这是一个常见的​​问题,但我尝试了很多的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.

我尝试了不同的codeS这样的:

I tried different codes like:

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

现在我有code:

    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.

这就是一个工作的例子:

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

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

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