如何在Android(NavigationDrawer)中更改汉堡包图标
在编写此线程之前,我已经尝试实现在stackoverflow中发现的不同解决方案,但是没有任何正常工作.
Before write this thread I have try to implement the different solution that I found in stackoverflow, but nothing work properly.
我正在开发使用自定义导航抽屉的Android应用,我必须更改操作栏的标准图标(现在是工具栏,对吗?)和设置图标.
I'm developing an Android applucation that use the custom navigation drawer, I have to change the standard icon of actionbar (now is toolbar, right ?), and settings icon.
这是我的代码:
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);
}
这就是我试图实现的目标:
And this is what i try to implement:
此解决方案不起作用:
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);
我不明白为什么我不能更改图标,我不知道出了什么问题...
I don't understand why i can't change the icon, i have no idea what's the problem...
简单而优雅的解决方案
放置
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();
导航活动中的总体解决方案
Overall Solution in Navigation Activity
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()
Note: It doesnt need any setNavigationOnClickListener()