java - 操作栏不显示项目

标签 java android android-actionbar android-4.4-kitkat

我知道之前已经被问过好几次了。但我想我必须自己问这个问题,因为现有的都不适合我的问题:

我正在为 Kitkat(4.4) 进行开发,并尝试使用主题为:holo.light.darkActionBar 的操作栏。

我使用菜单中的项目创建了一些 xml 文件,并将其在 java 类中链接到 Activity :

菜单/主菜单:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
<item android:id="@+id/action_filter"
    android:title="@string/action_filter"
    android:orderInCategory="100"
    android:icon="@drawable/ic_action_filter"
    app:showAsAction="ifRoom" />

主要 Activity :

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

问题如下: 当我单击 menu/main.xml 时,我可以看到一个项目已添加到操作栏中。但它从未出现在 Activity_main.xml 中。

我搜索了很多小时并考虑了以下内容: - 将 minSDK 设置为 19 -我知道有时它不会显示,因为设备有一个菜单按钮。但我使用一个图标并说应该显示它。因此它应该出现。我还使用没有菜单按钮的 Nexus Emulation。 -我使用Android studio并在android studio的帮助下插入 Activity 。 -mainActivity 扩展了 FragmentActivity(因为我选择了谷歌地图 Activity ) -我尝试了其他主题,但操作栏完全消失了(oc没有使用.noActionBar!) -我为操作栏设置了样式,并指定了红色。此颜色会传播到 Activity ,但不会传播到项目。

我的进口是:

import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.view.Menu;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;

它应该是什么样子: rendering the menu/main.xml

现在的样子: rendering the activity_main

我真的非常希望你们中的一些人能帮助我,这让我发疯。

最佳答案

我认为,您的菜单扩展代码应该如下所示:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}

并删除 menu.clear() 行。

关于java - 操作栏不显示项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30671583/

相关文章:

java - GreenRobot的eventbus看不到注解, "no public methods"

android - 如何让 Google Play 商店应用程序像搜索操作菜单和搜索对话框设计一样

java - 使用 Android 从 URL 获取 JSON 数据响应?

Java 程序输出应该是 true ,但它返回 false 为什么?

Java while 循环 | true 如果测试值返回异常错误

android - 如何更新 Room Dao 函数中实体的 TypeConverted 列

java - 使用 Gradle 添加提供的依赖项来测试类路径

android - 如何更新 Android 身份验证器模块 (AbstractAccountAuthenticator)?

c# - Xamarin Activity OnCreate ActionBar 为空

android - 使用 android.support.v7.app.ActionBar 的选项卡