Android 选项菜单不显示

发布于 2024-12-02 07:03:52 字数 1697 浏览 1 评论 0原文

我正在为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

半步萧音过轻尘 2024-12-09 07:03:52

确保所有菜单项都有标题

我想,没有标题的项目可以显示在操作栏中,但在按下菜单按钮时不能显示为列表。下面是一个简单的例子..

<menu xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/main_menu" >

    <item android:id="@+id/action_undo"
      android:icon="@drawable/ic_action_undo"
      android:title="@string/mb_undo"
      android:showAsAction="always"
      android:onClick="onPressUndo" />

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..

<menu xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/main_menu" >

    <item android:id="@+id/action_undo"
      android:icon="@drawable/ic_action_undo"
      android:title="@string/mb_undo"
      android:showAsAction="always"
      android:onClick="onPressUndo" />
凡间太子 2024-12-09 07:03:52

据我所知,如果代码和菜单资源文件一切正常,原因是没有为低级 API 添加额外的样式资源目录。

这是我的一套;

AndroidManifest.xml 文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.levent.learning.notetaker">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

A) MainActivity.java

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);

        return true;
    }

}

B) 菜单资源文件

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/hede"
        android:title="@string/hede_item"></item>
</menu>

C) 资源文件:styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="AppBaseTheme" parent="android:Theme.Light">
    </style>

    <style name="AppTheme" parent="AppBaseTheme">
    </style>
</resources>

这些看起来一切都很好,对吧?但即使有了这些,选项菜单也不可见。因此,我为 API 级别 11 创建了附加值资源目录,名为 value-v11,并为此添加了附加 styles.xml 文件。

解决方案 1

D) API 级别 11 的资源文件

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="AppBaseTheme" parent="android:Theme.Holo.Light">
    </style>
</resources>

那么它就可以工作了!

这是我的工作配置,为了为值资源创建两个不同的文件夹,我在 Android 中选择了 Project 选项;

输入图片此处描述

解决方案 2

AndroidManifest.xml 文件中删除以下主题定义行;

android:theme="@style/AppTheme"

这样新的AndroidManifest.xml如下;

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.levent.learning.notetaker">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

我是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

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.levent.learning.notetaker">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

A) MainActivity.java

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);

        return true;
    }

}

B) Resource File For Menu

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/hede"
        android:title="@string/hede_item"></item>
</menu>

C) Resource File : styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="AppBaseTheme" parent="android:Theme.Light">
    </style>

    <style name="AppTheme" parent="AppBaseTheme">
    </style>
</resources>

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

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="AppBaseTheme" parent="android:Theme.Holo.Light">
    </style>
</resources>

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;

enter image description here

SOLUTION 2

Remove the following theme definition line from AndroidManifest.xml file;

android:theme="@style/AppTheme"

So that the new AndroidManifest.xml is as follows;

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.levent.learning.notetaker">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

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!

檐上三寸雪 2024-12-09 07:03:52

我们可以直接添加这样的菜单项,它对我来说工作得很好

     @Override
        public boolean onCreateOptionsMenu(Menu menu) 
               {
    // TODO Auto-generated method stub
    super.onCreateOptionsMenu(menu);
    MenuItem item1=menu.add(0, 4, 0,"text1");
    item1.setIcon(R.drawable.car);
    MenuItem item2=menu.add(0, 0, 0, "text2");
    item2.setIcon(R.drawable.share);
    MenuItem item3=menu.add(0, 2, 0, "text3");
    item3.setIcon(R.drawable.history);
    MenuItem item4=menu.add(0, 3, 0, "text4");
    item4.setIcon(R.drawable.settings);
    return true;
}

Directly we can add menu items like this , it working fine for me

     @Override
        public boolean onCreateOptionsMenu(Menu menu) 
               {
    // TODO Auto-generated method stub
    super.onCreateOptionsMenu(menu);
    MenuItem item1=menu.add(0, 4, 0,"text1");
    item1.setIcon(R.drawable.car);
    MenuItem item2=menu.add(0, 0, 0, "text2");
    item2.setIcon(R.drawable.share);
    MenuItem item3=menu.add(0, 2, 0, "text3");
    item3.setIcon(R.drawable.history);
    MenuItem item4=menu.add(0, 3, 0, "text4");
    item4.setIcon(R.drawable.settings);
    return true;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文