Can't display an icon in the actionbar

2020-03-26 09:28发布

I'm trying to show a map icon downloaded from the official android developers source.

I did everything as it should, but the icon won't show. Here is my xml file named main_activity_bar:

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

    <item
        android:id="@+id/mapIcon"
        android:icon="@drawable/ic_action_map"
        android:title="@string/mapIconTitle"
        android:showAsAction="always"
     />

</menu>

Here is the main activity xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.gs.testApp.MainActivity"
    tools:ignore="MergeRootFrame" />

and this is what I have in the java class:

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater actionMenue = getMenuInflater();
        actionMenue.inflate(R.menu.main_activity_bar, menu);
        return super.onCreateOptionsMenu(menu);
    }

Everything seems to be fine, but the icon wont show on the emulator. Here is a screenshot:

enter image description here

The minimum version is android 3.0

Why the icon is not showing? What Am I missing? I know that it is something really small, but I can't spot it.

2条回答
\"骚年 ilove
2楼-- · 2020-03-26 09:59

I dont think an icon is shown in the overflow menu. Try changing your title to "t" or rotate the screen. See if the icon appears this way.

查看更多
家丑人穷心不美
3楼-- · 2020-03-26 10:02

Here is how I fixed it - in case that someone is facing the same issue.

I changed

android:showAsAction="always" to app:showAsAction="always" and I also placed the icon order android:orderInCategory="0" and the auto res xmlns:app="http://schemas.android.com/apk/res-auto" so now my xml looks like:

<?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">

    <item
        android:orderInCategory="0"
        android:id="@+id/mapIcon"
        android:icon="@drawable/ic_action_map"
        android:title="@string/mapIconTitle"
        app:showAsAction="always"
     />

</menu>
查看更多
登录 后发表回答