在工具栏中更改ActionBarDrawerToggle图标android?

我有一个带导航抽屉工具栏的活动

活动

public class MainActivity extends AppCompatActivity { private Toolbar toolbar; private CustomTextViewMondaRegular tvTitle; private ListView mDrawerList; private DrawerLayout mDrawerLayout; private ActionBarDrawerToggle mDrawerToggle; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); toolbar = (Toolbar) findViewById(R.id.tool_bar); setSupportActionBar(toolbar); mDrawerList = (ListView) findViewById(R.id.left_drawer); tvTitle = (CustomTextViewMondaRegular) findViewById(R.id.actionTitle); tvTitle.setText(getIntent().getStringExtra("title")); mDrawerList.setAdapter(new NavAdapter(getApplicationContext())); mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); mDrawerList.setOnItemClickListener(new DrawerItemClickListener()); mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,toolbar, R.string.title_activity_main, R.string.title_activity_main) { @Override public void onDrawerClosed(View view) { super.onDrawerClosed(view); invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() } @Override public void onDrawerOpened(View drawerView) { super.onDrawerOpened(drawerView); invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() } }; mDrawerLayout.setDrawerListener(mDrawerToggle); if (savedInstanceState == null) { } } @Override protected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); mDrawerToggle.syncState(); } public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); mDrawerToggle.onConfigurationChanged(newConfig); } private class DrawerItemClickListener implements ListView.OnItemClickListener { @Override public void onItemClick(AdapterView parent, View view, int position, long id) { Logger.e("pos", position + ""); } } 

}

活动XML

        

切换图标当前显示默认图标。 我需要改变那个图标。 我怎样才能做到这一点?

对于将来遇到这个SO问题的人,

我们必须禁用抽屉指示器

 mDrawerToggle.setDrawerIndicatorEnabled(false); 

然后设置ToolBar的导航按钮

  mToolbar.setNavigationIcon(R.drawable.navIcon); 

PS之后我们必须在工具栏上设置Navigation click listner并手动打开NavigationDrawer 。 像mToolbar.setNavigationOnClickListener :D

请尝试以下代码:

 getSupportActionBar().setHomeButtonEnabled(true); getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_menu_drawer); 
 mDrawerToggle.setDrawerIndicatorEnabled(false); 

然后打开抽屉

 mImageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { DrawerLayout drawer = findViewById(R.id.drawer_layout); drawer.openDrawer(Gravity.START); } }); 

您可以使用setHomeAsUpIndicator设置菜单图标,将drawable传递给此方法。

见下面的代码:

  ActionBar ab = getSupportActionBar(); ab.setHomeAsUpIndicator(R.drawable.ic_menu); ab.setDisplayHomeAsUpEnabled(true);