Action bar does not show

2019-02-02 20:46发布

问题:

I tried to implement an action bar in my application.

menu.xml

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

    <item
        android:id="@+id/itemAdd"
        android:showAsAction="ifRoom|withText"
        android:title="ADD">
    </item>
    <item
        android:id="@+id/itemRefresh"
        android:showAsAction="ifRoom|withText"
        android:title="REFRESH">
    </item>
    <item
        android:id="@+id/itemHelp"
        android:title="HELP">
    </item>

</menu>

And created menu

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

But it does not show the action bar even if minSdkVersion is 11. What is the reason?

回答1:

Remove your theme for your actionbar activity in androidManifest file. Now it will work...

<application
    android:allowBackup="true"
    android:icon="@drawable/tasktodo"
    android:label="@string/app_name"
    >

Don't add any theme in your application manifest file. If you added one, please remove and try running it...



回答2:

You have to set the style of your Activity to Theme.Holo or one of its variants for the ActionBar to show. If you want to keep backwards-compatibility, call setTheme in onCreate of your Activity:

setTheme(android.R.style.Theme_Holo);


回答3:

An application with a Manifest like this

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example.Actionbartest"
          android:versionCode="1"
          android:versionName="1.0">
    <uses-sdk android:minSdkVersion="7" android:targetSdkVersion="11" />
    <application android:label="@string/app_name" android:icon="@drawable/ic_launcher">
        <activity android:name="MyActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>
</manifest>

Menu.xml like this

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

    <item
            android:id="@+id/itemAdd"
            android:showAsAction="ifRoom|withText"
            android:title="ADD">
    </item>
    <item
            android:id="@+id/itemRefresh"
            android:showAsAction="ifRoom|withText"
            android:title="REFRESH">
    </item>
    <item
            android:id="@+id/itemHelp"
            android:title="HELP">
    </item>
</menu>

And Activity like this

package com.example.Actionbartest;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;

public class MyActivity extends Activity {
    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

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

Looks like this.

Are you sure your phone or emulator is running Android 3.0 or above? If not, you will end up with your screenshot.

To enable The Actionbar on older devices, you should use the AppCompat/support library (https://developer.android.com/tools/support-library/features.html)



回答4:

change Activity to AppCompatActivity in your class. That should be the easiest if you want to add it fast.. I'll add the code for someone who is new to Android OS:

public class YourActivity extends Activity 

into

public class YourActivity extends AppCompatActivity


回答5:

    android:allowBackup="true"
    android:icon="@drawable/ic2"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.Holo.Light.DarkActionBar" 

this works. Put it in your



回答6:

I import import android.support.v7.app.AppCompatActivity;

then edit to public class MainActivity extends AppCompatActivity

Add to dependencies

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