未在ActionBar中显示的项目,showAsAction =“always” [英] Items not showing in the ActionBar with showAsAction=“always”

查看:134
本文介绍了未在ActionBar中显示的项目,showAsAction =“always”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Android编程的新手,目前正试图让Actionbar正常工作。我的问题是,尽管我将XML文件中的项目设置为始终,但无论我尝试什么,它都会不断地出现在溢出菜单中。我在这里发现了几个类似的问题,但没有一个解决方案解决了这个问题。

I'm new to Android programming and currently trying to get the Actionbar working. My problem is, that although I set an item in the XML file to "always", it constantly ends up in the overflow menu no matter what I try. I found several similar problems on here, but none of the solutions fixed the problem.

其他信息:ic_action_search图标位于项目的可绘制文件夹中。

Additional info: The ic_action_search icons are in the drawable folders of the project.

main_activity_actions.xml:

main_activity_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, always displayed -->
<item android:id="@+id/action_search"
      android:icon="@drawable/ic_action_search"
      android:title="@string/action_search"
      app:showAsAction="always" />


<!-- Settings, should always be in the overflow -->
<item android:id="@+id/action_settings"
      android:title="@string/action_settings"
      app:showAsAction="never" />
</menu>

strings.xml:

strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>

  <string name="app_name">My First App</string>
  <string name="edit_message">Enter a message</string>
  <string name="button_send">Send</string>
  <string name="action_settings">Settings</string>
  <string name="action_search">Search</string>
  <string name="title_activity_main">MainActivity</string>
  <string name="title_activity_display_message">My Message</string>
  <string name="hello_world">Hello world!</string>
</resources>

在MainActivity.java中

In MainActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu items for use in the action bar
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main, menu);
    return super.onCreateOptionsMenu(menu);
}

感谢您的帮助!

推荐答案

尝试 android:showAsAction 而不是 app:showAsAction 。或者,如果您使用 appcompat_v7 backport,请同时使用 android:showAsAction app: showAsAction (感谢Commonsware)。

try android:showAsAction instead of app:showAsAction. Or if you're using the appcompat_v7 backport, use both android:showAsAction and app:showAsAction(Thanks to Commonsware).

这篇关于未在ActionBar中显示的项目,showAsAction =“always”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆