Android 选项菜单不显示
我正在为 Android 制作一个游戏,最简单的选项菜单实现不起作用 - 游戏本身位于一个单独的类(Engine.java)中,它扩展了 LinearLayout,所以我想知道这是否就是它不工作的原因可见 - 但是其他“顶级”@Overrides,如 onStop()、onPause() 等都很好 - 我需要在游戏类中做些什么才能让它显示选项菜单吗?
代码库来自 Pro Android Games SpaceBlaster 示例: http://www.apress.com/9781430226475 (源码可下载)
MyGame.class(应用程序入口点):
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutInflater factory = LayoutInflater.from(this);
// Set game layout
view = factory.inflate(R.layout.main, null);
setContentView(view);
// Enable view key events
view.setFocusable(true);
view.setFocusableInTouchMode(true);
} //End OnCreate()
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
res\menu\menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/icon"
android:icon="@drawable/icon" />
<item android:id="@+id/text"
android:title="Text" />
<item android:id="@+id/icontext"
android:title="Icon and text"
android:icon="@drawable/icon" />
</menu>
layout\main.xml:
<game.thegame.test.Engine
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll_absolute"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FF000000">
</game.thegame.test.Engine>
I am making a game for Android and the simplest implementation of an Options Menu doesn't work - the game itself is in a seperate class (Engine.java), that extends a LinearLayout, so I am wondering if that is why it is not visible - however the other "top-level" @Overrides like onStop(), onPause(), etc. all fire just fine - is there something I have to do inside my game class to get it to display an options menu?
The code base is from Pro Android Games SpaceBlaster example:
http://www.apress.com/9781430226475
(source can be downloaded)
MyGame.class (entrance point of application):
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutInflater factory = LayoutInflater.from(this);
// Set game layout
view = factory.inflate(R.layout.main, null);
setContentView(view);
// Enable view key events
view.setFocusable(true);
view.setFocusableInTouchMode(true);
} //End OnCreate()
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
res\menu\menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/icon"
android:icon="@drawable/icon" />
<item android:id="@+id/text"
android:title="Text" />
<item android:id="@+id/icontext"
android:title="Icon and text"
android:icon="@drawable/icon" />
</menu>
layout\main.xml:
<game.thegame.test.Engine
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll_absolute"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FF000000">
</game.thegame.test.Engine>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
确保所有菜单项都有标题!
我想,没有标题的项目可以显示在操作栏中,但在按下菜单按钮时不能显示为列表。下面是一个简单的例子..
Make sure you have a title in all your menu items!
Items without a title, I guess, can be displayed in the action bar but not as a list when pressing the menu button. Below is a simple example..
据我所知,如果代码和菜单资源文件一切正常,原因是没有为低级 API 添加额外的样式资源目录。
这是我的一套;
AndroidManifest.xml 文件
A) MainActivity.java
B) 菜单资源文件
C) 资源文件:styles.xml
这些看起来一切都很好,对吧?但即使有了这些,选项菜单也不可见。因此,我为 API 级别 11 创建了附加值资源目录,名为 value-v11,并为此添加了附加 styles.xml 文件。
解决方案 1
D) API 级别 11 的资源文件
那么它就可以工作了!
这是我的工作配置,为了为值资源创建两个不同的文件夹,我在 Android 中选择了
Project
选项;解决方案 2
从 AndroidManifest.xml 文件中删除以下主题定义行;
这样新的AndroidManifest.xml如下;
我是android开发的新手,使用Android Studio版本2.1.2。即使这个问题看起来很古老,但它仍然没有过时。我希望这个答案可以帮助那里的人,因为这个问题让我发疯,直到我找到经验解决方案,希望它有所帮助。快乐编码!
As far as I can see, if everything is fine with the code and menu resource files, the reason is not adding additional styles resource directory for a low level API.
Here is my set;
AndroidManifest.xml file
A) MainActivity.java
B) Resource File For Menu
C) Resource File : styles.xml
Everything seems fine with these right ? But even with these, Options menu is not visible. So I've created and additional values resource directory for API Level 11, named values-v11, and added a additional styles.xml file for that.
SOLUTION 1
D) Resource file for API Level 11
Then it works !
Here is my working configuration, to make two distinct folders for the values resource, I've selected the
Project
option in Android;SOLUTION 2
Remove the following theme definition line from AndroidManifest.xml file;
So that the new AndroidManifest.xml is as follows;
I'm a newbie on android development, using Android Studio version 2.1.2. Even this question is seems so old, it is still not obsolete. I hope this answer helps someone out there, cause this problem drove me crazy until I've found the empirical solution, hope that it helps. Happy coding!
我们可以直接添加这样的菜单项,它对我来说工作得很好
Directly we can add menu items like this , it working fine for me