如何在Android中更改汉堡图标(NavigationDrawer)

13

在撰写本帖之前,我尝试了在stackoverflow上找到的不同解决方案,但是没有一个能够正常工作。

我正在开发一款使用自定义导航抽屉的Android应用程序,我需要更改操作栏(现在是工具栏,对吗?)和设置图标的标准图标。

这是我的代码:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitleTextColor(Color.parseColor("#009754"));
        setSupportActionBar(toolbar);

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
    }

这是我尝试实现的内容:

这个解决方案不起作用:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitleTextColor(Color.parseColor("#009754"));
toolbar.setNavigationIcon(R.drawable.ic_draw);
setSupportActionBar(toolbar);

这个解决方案行不通:

getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(false);
toggle.setDrawerIndicatorEnabled(false);
toggle.setHomeAsUpIndicator(R.drawable.ic_custom_drawer_icon);

我不明白为什么我不能更改图标,我不知道问题出在哪里...

3个回答

22
简单而优雅的解决方案
位置
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.dashboardicon);//your icon here

之后

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer,toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();

导航活动的整体解决方案

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer,toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();

    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeAsUpIndicator(R.drawable.dashboardicon);

    navigationView= (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

注意:它不需要任何setNavigationOnClickListener()。


12

禁用 ActionBarDrawerToggle 的抽屉指示器:

toggle.setDrawerIndicatorEnabled(false);

然后:

toolbar.setNavigationIcon(R.drawable. ic_custom_drawer_icon);

1
然后,按照这里所述的方式,在工具栏上调用 setNavigationOnClickListener() 来添加自定义监听器。 - Mehmed
toggle.setDrawerIndicatorEnabled(false); 这将阻止该图标打开导航抽屉。toolbar.setNavigationIcon(R.drawable.ic_custom_drawer_icon); 应该足够了。 - Lekan

3
Just use this :

toolbar.post(new Runnable() {
            @Override
            public void run() {
                Drawable d = ResourcesCompat.getDrawable(getResources(), R.mipmap.ic_launcher, null);
                toolbar.setNavigationIcon(d);
            }
        });

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接