Action bar does not show

前端 未结 6 1132
不思量自难忘°
不思量自难忘° 2020-12-29 02:37

I tried to implement an action bar in my application.

menu.xml




        
相关标签:
6条回答
  • 2020-12-29 02:54

    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
    
    0 讨论(0)
  • 2020-12-29 02:55

    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)

    0 讨论(0)
  • 2020-12-29 03:03

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

    0 讨论(0)
  • 2020-12-29 03:04
        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

    0 讨论(0)
  • 2020-12-29 03:09

    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'
    }
    
    0 讨论(0)
  • 2020-12-29 03:15

    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);
    
    0 讨论(0)
提交回复
热议问题