Action bar does not show

2019-02-02 20:25发布

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

enter image description here

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

6条回答
狗以群分
2楼-- · 2019-02-02 21:12

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
查看更多
三岁会撩人
3楼-- · 2019-02-02 21:15

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...

查看更多
倾城 Initia
4楼-- · 2019-02-02 21:16

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'
}
查看更多
Emotional °昔
5楼-- · 2019-02-02 21:22

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);
查看更多
时光不老,我们不散
6楼-- · 2019-02-02 21:26

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.

enter image description here

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)

查看更多
该账号已被封号
7楼-- · 2019-02-02 21:26
    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

查看更多
登录 后发表回答