我正在学习 Android 开发。到目前为止,我已经创建了一个基本应用程序,我想显示一个 OptionMenu。我遵循了一个教程。 菜单没有出现在我的手机上。也许我忘记了什么?
我看过这篇文章: Option Menu does not appear in Android 但我的问题似乎有所不同。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_refresh) {
FetchWeatherTask weatherTask = new FetchWeatherTask();
weatherTask.execute();
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.forecastfragment, menu);
}
这是菜单的代码
<?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/action_refresh"
android:title="@string/action_refresh"
app:showAsAction="never" />
</menu>
我花了几个小时。从头开始重写代码 2 次,但问题仍然存在。 一切正常,除了菜单没有出现。 有什么问题?
最佳答案
在您的菜单 xml 中将最后一行更改为
app:showAsAction="ifRoom"
关于android - OptionMenu 不出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27463342/