Java Android 选项菜单未显示

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21214627/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 07:23:15  来源:igfitidea点击:

Android options menu not displaying

javaandroidxmlmenu

提问by user3119647

I'm new to Android and I've been trying to add a simple add button as mentioned below

我是 Android 新手,我一直在尝试添加一个简单的添加按钮,如下所述

list_menu.xml

list_menu.xml

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

MyActivity.java

我的活动.java

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        super.onCreateOptionsMenu(menu);
        getMenuInflater().inflate(R.menu.list_menu, menu);

        return true;  
    }

I read in Dummies series book that ic_menu_add is already there in resources and I don't need to add it, but when I run this code it does not display. I've tried to add a custom icon with same name still there is no button. Can someone help me with it please.

我在 Dummies 系列书中读到 ic_menu_add 已经存在于资源中,我不需要添加它,但是当我运行此代码时,它不显示。我尝试添加一个同名的自定义图标,但仍然没有按钮。有人可以帮我吗?

回答by amatellanes

It is not required to call super()method. Try replacing your onCreateOptionsMenufor that:

不需要调用super()方法。尝试替换你onCreateOptionsMenu的:

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

回答by MikeHelland

If you set your TargetSDK in the manifest to 5, the icon will show up.

如果您将清单中的 TargetSDK 设置为 5,则会显示该图标。

If you are targeting a newer Android SDK (3.0 and up) the action bar takes over the menu and by default doesn't display icons.

如果您的目标是更新的 Android SDK(3.0 及更高版本),操作栏会接管菜单,并且默认情况下不显示图标。

You can try this:

你可以试试这个:

How to show icons in ActionBar overflow menu?

如何在 ActionBar 溢出菜单中显示图标?

回答by RaiVikrant

I was dealing with the same problem.. read some queries and documentation.. Hope this might help you.

我正在处理同样的问题.. 阅读一些查询和文档.. 希望这可以帮助你。

Here's my XML file for a menu..

这是我的菜单 XML 文件..

<item
    android:id="@+id/action_send_feedback"
    android:orderInCategory="100"
    android:showAsAction="always"
    android:title="@string/action_send_feedback"/>

<item 
    android:id="@+id/action_share_app"
    android:orderInCategory="100"
    android:showAsAction="ifRoom"
    android:title="@string/action_share_app"
    android:icon="@drawable/ic_action_share" />

<item
    android:id="@+id/action_rate_app"
    android:orderInCategory="100"
    android:showAsAction="never"
    android:title="@string/action_rate_app"/>

JAVA Code goes here..

JAVA代码在这里..

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

For android phones which have option button (at the bottom of the phone) the menu item which are showAsAction="never" comes when the button is pressed.. or else they will be shown normally on the action bar options menu..

对于具有选项按钮(在手机底部)的 android 手机,按下按钮时会出现 showAsAction="never" 菜单项..否则它们将正常显示在操作栏选项菜单上..

Ref: http://developer.android.com/guide/topics/ui/menus.html#options-menu

参考:http: //developer.android.com/guide/topics/ui/menus.html#options-menu

回答by Roel

If you use a fragment then you need this in onCreate():

如果您使用片段,那么您需要在 onCreate() 中使用它:

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

回答by karthik kumar

Have you missed these lines in your xml file check once

您是否错过了 xml 文件中的这些行检查一次

xmlns:tools="http://schemas.android.com/tools"

tools:context=".MainActivity"

xmlns:tools="http://schemas.android.com/tools"

工具:上下文=“。MainActivity”

if you missed this is what causing problem for displaying option menu

如果您错过了这是导致显示选项菜单出现问题的原因

回答by Jyubin Patel

Hi hope below code is helpings to you:

嗨,希望以下代码对您有所帮助:

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorPrimary">@color/primary</item>
        <item name="colorPrimaryDark">@color/primaryDark</item>
    </style>

Please mention DarkActionBar as your Theme which you used into Android Manifest File.

请提及 DarkActionBar 作为您在 Android 清单文件中使用的主题。

回答by Thirumalvalavan

I have faced this issue. But in my case, I have added toolbar inside the Framelayout. Top of that I have added one more scroll view with match parent. Now ScrollView took the click control, not toolbar. So if you are using FrameLayout, your toolbar suppose to be the top most view.

我遇到过这个问题。但就我而言,我在 Framelayout 中添加了工具栏。最重要的是,我添加了一个带有匹配父级的滚动视图。现在 ScrollView 接受了点击控件,而不是工具栏。因此,如果您使用 FrameLayout,则您的工具栏应该是最顶部的视图。

回答by harsh kumar

Add

添加

app:showAsAction="always"

to menuitem.

到菜单项。

回答by Bonfix Ngetich

For me, I had to add the following code to the activity xml:

对我来说,我必须将以下代码添加到活动 xml 中:

 <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:theme="@style/AppTheme"
        app:popupTheme="@style/Theme.AppCompat.Light.DarkActionBar"
        app:title="@string/app_name"
        app:titleMarginStart="24dp"
        app:titleTextColor="@android:color/white" />

Then to the activity.java: onCreate

然后到activity.java:onCreate

Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

And

    @Override
public boolean onCreateOptionsMenu(Menu menu) {
    //super.onCreateOptionsMenu(menu);
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

And fragment onCreate:

和片段 onCreate:

 setHasOptionsMenu(true);