在Android上为多个工具栏设置菜单 [英] Set menus for multiple toolbars on android
本文介绍了在Android上为多个工具栏设置菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的活动中,我将以下代码用于两个工具栏.
In my activity I use the following code for my two toolbars.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Creating The Toolbar and setting it as the Toolbar for the activity
toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("My title");
toolbar2 = (Toolbar) findViewById(R.id.tool_bar_bottom);
setSupportActionBar(toolbar2);
...
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
我希望它对顶部工具栏使用menu_main.xml,对底部工具栏使用menu_bottom,但是对于顶部和底部工具栏,它都使用menu_main.xml.
I want it to use menu_main.xml for the top toolbar and menu_bottom for bottom toolbar but for both top and bottom toolbar it uses menu_main.xml.
有人可以解释如何正确执行吗?
Can somebody explain how to do it correctly?
推荐答案
当您使用两个ToolBar
时,请像这样设置菜单
As you are using two ToolBar
s set the menu like this
toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("My title");
上面的工具栏从onCreateOptionsMenu
扩展菜单,菜单CallBack侦听器将为onOptionsItemSelected
The above toolbar inflate menu from onCreateOptionsMenu
, menu CallBack listener will be onOptionsItemSelected
现在秒数ToolBar
toolbar2 = (Toolbar) findViewById(R.id.tool_bar_bottom);
toolbar2.inflateMenu(R.menu.bottom_menu);//changed
//toolbar2 menu items CallBack listener
toolbar2.setOnMenuItemClickListener(new OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem arg0) {
if(arg0.getItemId() == R.id.item_id){
}
return false;
}
});
这篇关于在Android上为多个工具栏设置菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文