更改导航抽屉汉堡包图标

我正在尝试更改NavigationView汉堡包菜单图标,但我无法这样做。

这是我到目前为止所尝试的

我有一个基本活动,其中导航抽屉设置完成。 这是相关的一段代码

 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.setContentView(R.layout.activity_base_nav); setSupportActionBar(toolbar); setupDrawer(); } private void setupDrawer() { mDrawerLayout.setDrawerListener(this); mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open, R.string.close); mDrawerToggle = new ActionBarDrawerToggle(mContext, mDrawerLayout, R.string.open, R.string.close); mDrawerLayout.setDrawerListener(mDrawerToggle); if (getSupportActionBar() != null) { getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(false); mDrawerToggle.setDrawerIndicatorEnabled(false); mDrawerToggle.setHomeAsUpIndicator(R.drawable.ic_share_48pt_2x); } mDrawerToggle.syncState(); mNavigationView.setNavigationItemSelectedListener( menuItem -> { mMenuItem = menuItem.getItemId(); mDrawerUtil.onNavMenuItemClicked(mMenuItem); mDrawerLayout.closeDrawers(); return true; }); } @Override public void setContentView(int layoutResID) { getLayoutInflater().inflate(layoutResID, mContainer); } @Override protected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); mDrawerToggle.syncState(); } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); mDrawerToggle.onConfigurationChanged(newConfig); } @Override public boolean onOptionsItemSelected(MenuItem item) { if (mDrawerToggle.onOptionsItemSelected(item)) { return true; } return super.onOptionsItemSelected(item); } 

但它似乎对我不起作用。 我也尝试在SupportActionBar上调用setDrawerIndicatorEnabled(false)setHomeAsUpIndicator(R.drawable.ic_share_48pt_2x) ,但这也不起作用。

    以下代码对我很有用,

     protected void onCreate(Bundle savedInstanceState) { ... getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(false); toggle.setDrawerIndicatorEnabled(false); toggle.setHomeAsUpIndicator(R.drawable.ic_custom_drawer_icon); ... } 

    我还必须添加一个工具栏导航点击监听器来监听自定义抽屉图标上的点击事件

     protected void onCreate(Bundle savedInstanceState) { ... toggle.setToolbarNavigationClickListener(new View.OnClickListener() { @Override public void onClick(View view) { DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); if (drawer.isDrawerOpen(GravityCompat.START)) { drawer.closeDrawer(GravityCompat.START); } else { drawer.openDrawer(GravityCompat.START); } } }); ... } 

    最后,我可以动态更新图标

     toggle.setHomeAsUpIndicator(R.drawable.ic_new_icon); 

    只需使用:

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

    您不需要处理已接受答案的setToolbarNavigationClickListener。

    您是否尝试查看文档? http://developer.android.com/training/implementing-navigation/nav-drawer.html

    我在其中find了这段代码:

     mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.ic_drawer, 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); getActionBar().setTitle(mTitle); invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() } /** Called when a drawer has settled in a completely open state. */ public void onDrawerOpened(View drawerView) { super.onDrawerOpened(drawerView); getActionBar().setTitle(mDrawerTitle); invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() } }; 

    这对我有用:

     override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) setSupportActionBar(toolbar) val toggle = ActionBarDrawerToggle( this, drawer_layout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close) //toggle.isDrawerSlideAnimationEnabled = false toggle.isDrawerIndicatorEnabled = false toggle.setToolbarNavigationClickListener { if (drawer_layout.isDrawerOpen(GravityCompat.START)) drawer_layout.closeDrawer(GravityCompat.START) else drawer_layout.openDrawer(GravityCompat.START) } toggle.setHomeAsUpIndicator(AppCompatResources.getDrawable(this, R.drawable.ic_android_black_24dp)) drawer_layout.addDrawerListener(toggle) toggle.syncState() nav_view.setNavigationItemSelectedListener(this) } override fun onBackPressed() { if (drawer_layout.isDrawerOpen(GravityCompat.START)) { drawer_layout.closeDrawer(GravityCompat.START) } else { super.onBackPressed() } } override fun onNavigationItemSelected(item: MenuItem): Boolean { when (item.itemId) { ... } drawer_layout.closeDrawer(GravityCompat.START) return true }