[英]onCreateOptionsMenu showing alwasy as action with a menu that i have created programatically
[英]Menu showing but onCreateOptionsMenu() not called
該菜單在應用程序的工具欄中可見,我可以打開它並查看菜單項,但是沒有調用onCreateOptionsMenu()
函數,我通過設置斷點和調試知道這一點, onOptionsItemSelected()
函數,它根本沒有被調用。 我看過其他關於相同問題的堆棧溢出帖子,但我似乎沒有犯任何常見的錯誤。 有誰知道問題是什么以及如何解決它?
主活動.java
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId())
{
case R.id.menuItem1:
Toast.makeText(this, "menuItem1 selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.menuItem2:
Toast.makeText(this, "menuItem2 selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.menuItem3:
Toast.makeText(this, "menuItem3 selected", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
菜單文件
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/menuItem1"
android:title="One"
app:showAsAction="never"/>
<item
android:id="@+id/menuItem2"
android:title="Two"
app:showAsAction="never"/>
<item
android:id="@+id/menuItem3"
android:title="Three"
app:showAsAction="never"/>
</menu>
編輯:在 MainActivity.java 的onCreate()
中,我有這行代碼:
toolBar.inflateMenu(R.menu.menu);
如果我刪除這一行,工具欄中的菜單就會消失。 不知道這是否對我的問題有幫助或有什么關系,但只是放在那里讓你知道。
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
在 oncreate 函數中,放置這一行:
setSupportActionBar(bottomAppBar)
有關更多詳細信息,請參閱此代碼: https : //github.com/ranger163/BottomAppBarImplementation
setSupportActionBar(toolBar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
並使用android.support.v7.widget.Toolbar
會工作
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.