安卓菜单问题

发布于 2025-01-07 09:32:16 字数 1349 浏览 2 评论 0原文

我正在尝试创建一个菜单项,以便当用户单击手机上的菜单按钮时,它会显示此菜单。我的代码正在编译,它显示一个菜单,但不显示与菜单按钮关联的图像或文本。

我将图像放在文件夹 res/drawable/inage1icon.png 中 知道问题是什么吗?

下面是代码

package com.webview;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.MenuInflater;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.view.Menu;
import android.view.MenuInflater;


public class WebViewActivity extends Activity { 

WebView mWebView; 

public boolean onCreateoptionsMenu(Menu menu){
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.mainmenu, menu)
    return true;
    }
}

mainmenu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/buttoneone" 
      android:icon="@drawable/image1icon"
      android:title="@string/showimage1" />
</menu>

strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, WebViewActivity!</string>
<string name="app_name">WebView</string>
<string name="showimage1">IMAGE ONE</string>

<color name="background">#000000</color>

</resources>

Im trying to create a menu item so when a user clicks on the menu button on their phone it displays this menu. My code is compiling and its displaying a menu but not the image or text assoiciated with the menu button.

I have the image in a folder res/drawable/inage1icon.png
Any Idea what the issue is?

Below is the code

package com.webview;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.MenuInflater;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.view.Menu;
import android.view.MenuInflater;


public class WebViewActivity extends Activity { 

WebView mWebView; 

public boolean onCreateoptionsMenu(Menu menu){
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.mainmenu, menu)
    return true;
    }
}

mainmenu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/buttoneone" 
      android:icon="@drawable/image1icon"
      android:title="@string/showimage1" />
</menu>

strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, WebViewActivity!</string>
<string name="app_name">WebView</string>
<string name="showimage1">IMAGE ONE</string>

<color name="background">#000000</color>

</resources>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

橘寄 2025-01-14 09:32:16

这是复制和粘贴错误,还是您的代码在应用程序中的方式?

您拼错了方法名称。它应该是:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.mainmenu, menu)
    return true;
}

而您有 public boolean onCreateoptionsMenu(Menu menu)。另外,返回超类的方法也是一个好主意;不要说return true,而是说return(super.onCreateOptionsMenu())

编辑:此外,如果您在 Eclipse 中进行开发,如果您使用快捷键 Cmd+Opt+s 并选择覆盖/实现方法,则可以确保不会发生此类拼写错误。在该菜单中,Eclipse 将列出您扩展的类中的所有方法。

Is this a copying and pasting error, or is this the way your code is within the app?

You've misspelled the method name. It should be:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.mainmenu, menu)
    return true;
}

whereas you have public boolean onCreateoptionsMenu(Menu menu). Also, it is a good idea to return the superclass's method; instead of saying return true, say return(super.onCreateOptionsMenu()).

EDIT: Also, if you are developing in Eclipse, you can ensure that spelling errors such as this won't occur if you use the shortcut Cmd+Opt+s and select Override/Implement Methods. In that menu, Eclipse will list all the methods in the class you have extended.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文