操作栏不显示菜单项

时间:2023-01-16 14:14:16

(Please read before tagging as duplicated)

(请在标记为重复之前阅读)

I know this question has been asked millions of times, but in my case it doesn't have anything to do with the "ShowAsAction" property on the item, I applied a personal theme to my actionbar, I have done this several times before, and never had any problems with the items on the menu at the actionbar. But in this particular project they have disappeared, and even if I set the default apptheme as Theme.AppCompat.Light.DarkActionBar, still not getting any items shown.

我知道这个问题被问了好几百万次,但在我的情况下,它与项目的“ShowAsAction”属性没有任何关系,我将个人主题应用到我的操作栏,我之前做了好几次,并且从未在操作栏上的菜单上显示任何问题。但是在这个特定的项目中,它们已经消失了,即使我将默认的apptheme设置为Theme.AppCompat.Light.DarkActionBar,仍然没有显示任何项目。

By the way I'm using Model–view–controller (MVC) Methodology on my design and development of this project (Please forgive my English is not my native language )

顺便说一句,我正在使用模型 - 视图 - 控制器(MVC)方法论来设计和开发这个项目(请原谅我的英语不是我的母语)

Ill post all the code involved.

生病了所有涉及的代码。

First my AndroidManifest.xml

首先是我的AndroidManifest.xml

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

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

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

    <activity android:name=".SubCategoryActivity"
        android:label="@string/subcategory_activity_title"
        android:windowSoftInputMode="stateAlwaysHidden">

    </activity>

    <activity android:name=".ItemActivity"
        android:label="@string/items_activity_title"
        android:windowSoftInputMode="stateAlwaysHidden">

    </activity>
</application>

</manifest>

As i'm using the MVC model, the fragment is the one who inflates the menu (Done this several times with 100% success showing menu items on other projects)

因为我正在使用MVC模型,片段是膨胀菜单的人(完成了几次,100%成功显示其他项目的菜单项)

CategoryListFragment.java

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import java.util.List;

/**
* Created by JL on 24/04/2015.
*/
public class CategoryListFragment extends ListFragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,   Bundle savedInstanceState) {


    return inflater.inflate(R.layout.fragment_categories_list, container, false);


}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_main, menu);
}

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

    List<CategoryEntity> categories =   CategoryFactory.getInstance().getCategories();
    loadCategories(categories);
}
private void loadCategories(List<CategoryEntity> categories) {

    CategoryItemAdapter categoryItemAdapter =  new  CategoryItemAdapter(getActivity(),categories);

    setListAdapter(categoryItemAdapter);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {

    CategoryItemAdapter categoryItemAdapter= (CategoryItemAdapter)  l.getAdapter();
    CategoryEntity categoryEntity= (CategoryEntity)  categoryItemAdapter.getItem(position);


    Intent intent = new Intent(getActivity(),SubCategoryActivity.class);
    intent.putExtra(BundleKeys.KEY_CATEGORY_SUB_ID,          categoryEntity.getId_category());



   startActivity(intent);
}}

Now this is the "menu_main" that I'm trying to inflate:

现在这是我试图膨胀的“menu_main”:

menu_main.xml

<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_settings"
    android:icon="@drawable/search_icon"
    android:orderInCategory="100"
    app:showAsAction="always"
    android:title="@string/action_search"
 />

<!-- I've tried changing the ShowAsAction to IfRoom so that's
not the problem-->
</menu>

(The androidStudio's preview of this menu_main.xml doesn't show the items on the menu either)

(androidStudio的这个menu_main.xml的预览也没有显示菜单上的项目)

Now I have this on my Style resources: As you will see I've added support-library-compatibility to the items that needed it, (again, done this several times before)

现在我在我的Style资源上有这个:正如您将看到我已经为需要它的项添加了支持库兼容性(再次,之前已经多次完成)

styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="MyNewTheme">
    <!-- Customize your theme here. -->
</style>
<color name="white">#ffff</color>
<color name="black">#0000</color>

<style name="MyNewTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="actionBarSize">50dp</item>

    <item name="android:windowBackground">@color/white</item>
    <item name="android:colorBackground">@color/white</item>

    <item name="android:actionBarStyle">@style/MyNewActionBar</item>
    <item name="android:actionBarTabStyle">@style/MyActionBarTabs</item>
    <!-- Support library compatibility -->
    <item name="actionBarStyle">@style/MyNewActionBar</item>
    <item name="actionBarTabStyle">@style/MyActionBarTabs</item>



</style>

<style name="MyNewActionBar"   parent="@style/Widget.AppCompat.Light.ActionBar">
    <item name="android:background">@drawable/action_bar_bg_1</item>
    <!--<item name="android:titleTextStyle">@style/NoTitleText</item>-->
    <!--<item name="android:subtitleTextStyle">@style/NoTitleText</item>-->

    <item name="android:titleTextStyle">@style/NewTitle</item>
    <item name="android:subtitleTextStyle">@style/NewTitle</item>

    <!-- Support library compatibility -->
    <item name="background">@drawable/action_bar_bg_1</item>
    <item name="titleTextStyle">@style/NewTitle</item>
    <item name="subtitleTextStyle">@style/NewTitle</item>

</style>

<style name="MyActionBarTabs"  parent="@style/Widget.AppCompat.ActionBar.TabView">

    <item name="android:height">25dp</item>
    <item name="android:background">@drawable/action_bar_tab_indicator_1</item>

    <!-- Support library compatibility -->
    <item name="background">@drawable/action_bar_tab_indicator_1</item>
</style>

<style name="NoTitleText">
    <item name="android:textSize">0sp</item>
    <item name="android:textColor">#00000000</item>
    <!-- Support library compatibility -->

</style>

<style name = "NewTitle" >
    <item name="android:textSize">20sp</item>
    <item name="android:textColor">#ffff</item>


</style>

<style name="BasicTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="actionBarSize">50dp</item>


</style>
</resources>

And finally this is my Gradle Script, this is where I think, the error must be, I personally work with an minSdkVersion 9, this way I cover most of the android OS from 2.3 to 5.

最后这是我的Gradle脚本,这是我认为的,错误必须是,我个人使用minSdkVersion 9,这样我覆盖了2.3到5的大部分Android操作系统。

build.gradle app

apply plugin: 'com.android.application'

android {
compileSdkVersion 22
buildToolsVersion "22.0.1"

defaultConfig {
    applicationId "com.prometheus.projectz.projectz"
    minSdkVersion 9
    targetSdkVersion 22
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'),   'proguard-rules.pro'
    }
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.1.0'
compile 'com.android.support:support-v4:22.1.0'
}

I've Worked 1 week on this project, and I really don't want to start over, because of this "BUG". PLEASE DON'T MARK THIS QUESTION AS DUPLICATED CAUSE IT'S NOT THE USUALLY ShowAsAction MISTAKE.

我在这个项目上工作了一周,我真的不想重新开始,因为这个“BUG”。请不要将此问题标记为重复原因,这不是通常的ShowAsAction错误。

I would really like to post some screenshots but I don't have the needed reputation yet.

我真的想发布一些截图,但我还没有所需的声誉。

1 个解决方案

#1


You are missing following function in your Fragment that indicates this Fragment has a menu.

您在片段中缺少以下功能,表示此片段具有菜单。

setHasOptionsMenu(true);

Call this function in your onCreateView function.

在onCreateView函数中调用此函数。

http://developer.android.com/reference/android/app/Fragment.html#setHasOptionsMenu(boolean)

#1


You are missing following function in your Fragment that indicates this Fragment has a menu.

您在片段中缺少以下功能,表示此片段具有菜单。

setHasOptionsMenu(true);

Call this function in your onCreateView function.

在onCreateView函数中调用此函数。

http://developer.android.com/reference/android/app/Fragment.html#setHasOptionsMenu(boolean)