无法在操作栏中显示图标

3

我尝试展示一个从官方Android开发者网站下载的地图图标。

我按照要求做了一切,但是图标无法展示。 这里是我的xml文件,名为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>

这是主活动的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" />

以下是 Java 类中的代码:

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

一切看起来都很好,但图标在模拟器上不显示。以下是屏幕截图:

enter image description here

最低版本为Android 3.0。

为什么图标没有显示?我错过了什么吗?我知道这可能是一个非常小的问题,但我找不到它。


点击三个点的溢出菜单图标,可以查看操作栏图标。如果空间不足,则会在溢出菜单中显示。 - Raghunandan
1
@Raghunandan 当我点击三个点时,我只能看到我的标题 test。在上面的截图中它已经被点击了。 - chility
引用Romain Nurik的话:“在操作栏溢出菜单中不显示图标的行为是有意设计的。” - Raghunandan
2个回答

2
以下是我如何解决这个问题的方法 - 如果有人面临同样的问题,请参考。
我将 android:showAsAction="always" 更改为 app:showAsAction="always" ,并在图标顺序中加入了 android:orderInCategory="0" 和自动资源 xmlns:app="http://schemas.android.com/apk/res-auto" ,所以现在我的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">

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

</menu>

0

我认为溢出菜单中不会显示图标。尝试将标题更改为“t”或旋转屏幕。看看是否以这种方式出现图标。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接