图标未显示在操作栏中

时间:2017-12-26 20:54:16

标签: android android-layout android-menu

我正在尝试在我的Android应用中使用操作栏来显示图标。无论我做什么,我只看到带下拉列表的溢出图标,而我想在操作栏菜单上看到彼此相邻的Home和Logout图标。我已阅读SO上的大多数帖子以获得解决方案,但无法找到解决方案。有人可以帮忙吗?

以下是我的代码:

的AndroidManifest.xml

<application
    android:name=".application.MySampleApplication"
    android:icon="@drawable/letter"
    android:label="My Sample"
    android:theme="@android:style/Theme.WithActionBar">

Menu_Main.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:id="@+id/action_logout"
        android:icon="@drawable/logout"
        android:title="@string/logout"
        android:orderInCategory="0"
        app:showAsAction="always" />
    <item
        android:id="@+id/action_home"
        android:title="@string/gohome"
        android:icon="@drawable/ulogo"
        android:orderInCategory="0"
        app:showAsAction="always" />
</menu>

MainActivity.java

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.action_logout) {
        logout();
        return true;
    }
    if (id == R.id.action_home) {
        goHome();
        return true;
    }
    return super.onOptionsItemSelected(item);
}

现在如何显示 How it shows up now

我希望它如何显示

Need to show

1 个答案:

答案 0 :(得分:1)

  

MainActivity扩展活动

这意味着您使用的是本机操作栏,而不是appcompat-v7提供的后端。在这种情况下,请将菜单资源中的app:前缀替换为android:,您将获得更好的运气。

  

谷歌搜索解决方案并看到有人使用它

这令人惊讶。你的问题是我第一次看到有人使用这个主题。我甚至不知道它存在,直到我查看它作为检查你的问题的一部分。最有可能的是,你会想要使用更现代的主题。