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

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

问题描述

在新的更新谷歌已经发布了一个新的API支持库,支持动作条在API级别7+。

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

我用ActionBarSherlock直到这个更新,我写了code加载菜单:

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>

要设置菜单按钮的操作栏。这code完美工作与ActionBarSherlock。但是,当我改变了操作栏到新的支持库,该按钮在操作栏中显示。即使他们被设置为安卓showAsAction =总是。当我调试code,功能 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?

推荐答案

尝试pressing您的设备或仿真器上的MENU按钮,看看他们是否出现溢出。

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

如果他们这样做,那么问题是,你的&LT;菜单&gt; XML需要改变。菜单XML与ActionBarSherlock和本机API级别11+操作栏的作品将不会与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 <​​/ code> preFIX为别的相关的操作栏(例如, yourapp:actionLayout )。

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

您可以看到这包括在操作栏文档

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

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