ActionBar没有在android中显示图标

时间:2014-06-17 12:48:46

标签: android android-actionbar

我一直试图在动作栏中显示图标,但我无法弥补它,请指导我并指出我出错的地方。这是我的代码

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>

enter image description here

上图显示了我得到的结果。但是我想在操作栏上找到搜索图标。

2 个答案:

答案 0 :(得分:1)

使用 app:showAsAction="always"代替android:showAsAction="always"

答案 1 :(得分:0)

您必须使用R.menu,因为您的菜单xml属于菜单文件夹: -

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