春衣少年当酒歌,起舞四顾以笑和。这篇文章主要讲述如何更改Android中的汉堡包图标(导航抽屉)相关的知识,希望能为你提供帮助。
在编写此线程之前,我已尝试实现我在stackoverflow中找到的不同解决方案,但没有任何正常工作。
我正在开发一个使用自定义导航抽屉的android应用程序,我必须更改操作栏上的标准图标(现在是工具栏,对吧?)和设置图标。
【如何更改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);
我不明白为什么我不能改变图标,我不知道是什么问题......
答案简单而优雅的解决方案
地点
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.dashboardicon);
//你的图标在这里后
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()
另一答案禁用
ActionBarDrawerToggle
的抽屉指示器:toggle.setDrawerIndicatorEnabled(false);
然后:
toolbar.setNavigationIcon(R.drawable. ic_custom_drawer_icon);
推荐阅读
- Android Room持久性库@Update无效
- 如何从android API 23和更高版本的操作栏中删除阴影
- 在动作栏android中显示徽标和应用程序名称
- Android棒棒糖中的透明actionBar和statusBar
- Xamarin - 取代操作栏(Android 7.1 - API 25)
- android中的pidstat命令()
- 如何在android中实现视图滑动
- java.lang.NoClassDefFoundError(解析失败:Lcom / google / android / gms / common / internal / zzbp)
- 使用activity方法后,Android应用程序与RabbitMQ断开连接