简体   繁体   中英

ActionBar is not displaying icons in android

I have been trying to display icons in the Actionbar but i could not make it up please guide me and point me out where I am going wrong. Here is my code

acivity_main_actions.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">
<!-- Search / will display always -->
<item android:id="@+id/action_search"
      android:icon="@drawable/ic_action_search"
      android:title="@string/action_search"
      android:showAsAction="always"/>

<!-- Location Found -->
<item android:id="@+id/action_location_found"
      android:icon="@drawable/ic_action_location_found"
      android:title="@string/action_location_found"
      android:showAsAction="ifRoom" />

<!-- Refresh -->
<item android:id="@+id/action_refresh"
      android:icon="@drawable/ic_action_refresh"
      android:title="@string/action_refresh"
      android:showAsAction="ifRoom" />

<!-- Help -->
<item android:id="@+id/action_help"
      android:icon="@drawable/ic_action_help"
      android:title="@string/action_help"
      android:showAsAction="never"/>

<!-- Check updates -->
<item android:id="@+id/action_check_updates"
      android:icon="@drawable/ic_action_refresh"
      android:title="@string/action_check_updates"
      android:showAsAction="never" />
</menu>    

MainActivity.java

public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
     MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.layout.activity_main_actions, menu);

        return super.onCreateOptionsMenu(menu);

}

AndroidManifest.xml

<uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="19" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.ThrashingBlue.example.entertain.MainActivity"
        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>

在此处输入图片说明

The above figure show the result that I got. But I want the search icon on the actionbar.

使用app:showAsAction="always" insted的的android:showAsAction="always"

You have to use R.menu because your menu xml belongs to menu folder:-

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu, menu);
    return true;
}

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