android.support.v7 with `ActionBarActivity` 没有菜单显示 [英] android.support.v7 with `ActionBarActivity` no menu shows

查看:17
本文介绍了android.support.v7 with `ActionBarActivity` 没有菜单显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在新的更新中,Google 发布了一个新的 API 支持库,该库支持 API 级别 7+ 中的 ActionBar.

In the new update Google has released a new API support library, that supports the ActionBar in API level 7+.

我使用 ActionBarSherlock 直到这次更新,我编写了加载菜单的代码:

I used ActionBarSherlock until this update and I wrote the code to load the menu:

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

和菜单文件:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/item_menu_ok" android:icon="@drawable/ic_action_ok"
        android:title="@string/ok" android:showAsAction="always"></item>
    <item android:id="@+id/item_menu_cancel" android:icon="@drawable/ic_action_cancel"
        android:title="@string/cancel" android:showAsAction="always"></item>
</menu>

在操作栏上设置菜单按钮.此代码与 ActionBarSherlock 完美配合.但是当我将操作栏更改为新的支持库时,操作栏中没有显示按钮.即使它们被设置为 android:showAsAction="always".当我调试代码时,函数 menu.getSize() 返回 2,这是正确的,但没有显示任何按钮..

To set up the menu buttons on the action bar. This code worked perfectly with ActionBarSherlock. But when I changed the action bar to the new support library, the buttons are not shown in the action bar. Even if they are set as android:showAsAction="always". And when I debug the code, the function menu.getSize() return 2, and that is correct, but no buttons are shown..

为什么按钮没有显示在新的支持库中?

Why are the buttons not shown in the new support library?

推荐答案

尝试按下设备或模拟器上的 MENU 按钮,看看它们是否出现在溢出中.

Try pressing the MENU button on your device or emulator, and see if they appear in the overflow.

如果他们这样做了,那么问题在于您的

XML 需要更改.适用于 ActionBarSherlock 和本机 API 级别 11+ 操作栏的菜单 XML 不适用于 AppCompat 操作栏向后移植.

If they do, then the problem is that your <menu> XML needs to change. Menu XML that works with ActionBarSherlock and the native API Level 11+ action bar will not work with the AppCompat action bar backport.

您的菜单 XML 需要如下所示:

Your menu XML would need to look like this:

<?xml version="1.0" encoding="utf-8"?>
<menu
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:yourapp="http://schemas.android.com/apk/res-auto"
>
    <item android:id="@+id/item_menu_ok" android:icon="@drawable/ic_action_ok"
        android:title="@string/ok" yourapp:showAsAction="always"></item>
    <item android:id="@+id/item_menu_cancel" android:icon="@drawable/ic_action_cancel"
        android:title="@string/cancel" yourapp:showAsAction="always"></item>
</menu>

并且您需要对与操作栏相关的任何其他内容使用相同的 yourapp 前缀(例如,yourapp:actionLayout).

And you would need to use the same yourapp prefix for anything else related to the action bar (e.g., yourapp:actionLayout).

您可以在操作栏文档中看到这一点.

You can see this covered in the action bar documentation.

这篇关于android.support.v7 with `ActionBarActivity` 没有菜单显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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