Android 工具栏操作图标不起作用
Android Toolbar Action icon not working
我的应用程序中有两个 Material 工具栏,还有两个 menu_main.xml
和 menu_main2.xml
。该图标在两个工具栏中均正确显示,但其中一个工具栏上的操作不起作用。我该如何解决?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Setup Toolbar
toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
toolbar2 = (Toolbar) findViewById(R.id.tool_bar2);
toolbar2.inflateMenu(R.menu.menu_main2);
这是我的 onCreateOptions,
@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;
}
和 onOptionsItemSelected。
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
web1 = (WebView) findViewById(R.id.web1);
web2 = (WebView) findViewById(R.id.web2);
web3 = (WebView) findViewById(R.id.web3);
这个动作有效,它来自工具栏,
if (id == R.id.action_google) {
web1.setVisibility(View.GONE);
web2.setVisibility(View.GONE);
web3.setVisibility(View.VISIBLE);
}
这是来自 Toolbar2,它不起作用...
if (id == R.id.action_naver) {
web1.setVisibility(View.VISIBLE);
web2.setVisibility(View.GONE);
web3.setVisibility(View.GONE);
}
只有第一个工具栏会被 onOptionsItemSelected 选中,因为这是您设置为 supportActionBar 的唯一工具栏。对于其他工具栏,您必须为您的项目设置点击监听器。正如 heloisasim 所建议的,您可以使用 setOnMenuItemClickListener 方法来执行此操作。
toolbar2.setOnMenuItemClickListener(new OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem arg0) {
if(arg0.getItemId() == R.id.whatever){
}
return false;
}
});
我的应用程序中有两个 Material 工具栏,还有两个 menu_main.xml
和 menu_main2.xml
。该图标在两个工具栏中均正确显示,但其中一个工具栏上的操作不起作用。我该如何解决?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Setup Toolbar
toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
toolbar2 = (Toolbar) findViewById(R.id.tool_bar2);
toolbar2.inflateMenu(R.menu.menu_main2);
这是我的 onCreateOptions,
@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;
}
和 onOptionsItemSelected。
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
web1 = (WebView) findViewById(R.id.web1);
web2 = (WebView) findViewById(R.id.web2);
web3 = (WebView) findViewById(R.id.web3);
这个动作有效,它来自工具栏,
if (id == R.id.action_google) {
web1.setVisibility(View.GONE);
web2.setVisibility(View.GONE);
web3.setVisibility(View.VISIBLE);
}
这是来自 Toolbar2,它不起作用...
if (id == R.id.action_naver) {
web1.setVisibility(View.VISIBLE);
web2.setVisibility(View.GONE);
web3.setVisibility(View.GONE);
}
只有第一个工具栏会被 onOptionsItemSelected 选中,因为这是您设置为 supportActionBar 的唯一工具栏。对于其他工具栏,您必须为您的项目设置点击监听器。正如 heloisasim 所建议的,您可以使用 setOnMenuItemClickListener 方法来执行此操作。
toolbar2.setOnMenuItemClickListener(new OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem arg0) {
if(arg0.getItemId() == R.id.whatever){
}
return false;
}
});