简体   繁体   中英

Actionbar does not show items

I know it has been asked several times before. But I guess I have to ask the question myself because none of the existing ones fits my problem:

I am developing for Kitkat(4.4) and try to use a action bar with the theme: holo.light.darkActionBar.

I created some xml files with the items in menu and linked it in the java class to the activity:

menu/main:

<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" />

MainActivity:

@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;
}

The Problem is the following: When I click on the menu/main.xml I can see that a item is added to the actionbar. But it never appears in the activity_main.xml.

I searched for many hours and considered the following: -set minSDK to 19 -i know that some time it does not show up because the device has a menu button. But I use a icon and say it should be displayed. Therefore it should show up. And I also use a Nexus Emulation which has not menu button. -I use Android studio and inserted the activities with the assistentce of android studio. -The mainActivity extends FragmentActivity(because I chose to have a google maps activity) -I tried other themes but then the actionbar disappears totally(oc did not use .noActionBar!) -I set a style for the actionbar and there I specify a red color. This color is propagated to the activity but not the items.

my imports are:

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;

How it should look: 渲染menu / main.xml

How it looks rightnow: 渲染activity_main

I really really hope some of you can help me, it drives me nuts.

I think, your code to inflate the menu should look like this :

@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);
}

And remove menu.clear() line.

try to use a action bar with the theme: holo.light.darkActionBar

That means that you are trying to use the native action bar.

I created some xml files with the items in menu and linked it in the java class to the activity

Get rid of the app namespace, and rename app:showAsAction to be android:showAsAction . The app convention is used for the appcompat-v7 action bar backport, which you are not using.

MainActivity:

Get rid of menu.clear() .

But it never appears in the activity_main.xml.

It is not supposed to. It should show up when you run the app, not necessarily in editors for other resources.

And I also use a Nexus Emulation which has not menu button

There certainly were Nexus devices with MENU buttons. Try pressing PgUp ; if you get the overflow menu rising from the bottom of the screen, your emulator has a MENU button.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM