使用AppCompat时无法显示Actionbar

33

我正在使用AppCompat将我的应用回溯到API7,但是在操作栏方面遇到了问题。

当我使用FragmentActivity时,在我的手机上(API18)会显示操作栏,但是当我使用ActionBarActivity时,它会在按下菜单按钮后显示为选项菜单。

在API7的模拟器上,操作栏始终显示为选项菜单。

有什么想法吗?

3个回答

70

像这样在您的菜单项中使用兼容名称空间:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:compat="http://schemas.android.com/apk/res-auto" >
    <item android:id="@+id/action_whatever"
      android:icon="@drawable/ic_action_whatever"
      android:title="@string/whatever"
      compat:showAsAction="ifRoom"  />
</menu>

6
指向此帖子的重复帖子相关,尽管showAsAction设置为 always ,但我无法使按钮显示为操作项而不是溢出项。 我通过使用 Activity 而不是 ActionBarActivity 扩展我的活动来强制执行它。 根据这个答案,如果您不需要支持低于11的API级别,则可以接受此方法。 ...扩展ActionBarActivity extends ActionBarActivity ...扩展Activity enter image description here

0

我使用Doogee Valencia Y100Pro进行调试,但是菜单中的“三个小方块”不可见。当我将MainActivity扩展为android.support.v7.app.ActionBarActivity时,我就可以在操作栏中获得文本/图标菜单了。接下来是screenshot和menu.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_update"
        android:icon="@drawable/ic_refresh"
        android:title="@string/action_update"
        app:showAsAction="always"/>
    <item android:id="@+id/action_settings"
        android:title="@string/action_settings"
        app:showAsAction="ifRoom"/>
</menu>

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