ActionBarCompat菜单项未显示 [英] ActionBarCompat menu item is not showing

查看:111
本文介绍了ActionBarCompat菜单项未显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的 general.xml 文件

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="@string/action_settings"/>
    <item 
        android:id="@+id/next" 
        android:title="Next" 
        android:visible="true" 
        android:enabled="true" 
        android:showAsAction="always" 
        android:orderInCategory="1">

    </item>
    <item 
        android:id="@+id/Previous"
        android:title="Previous" 
        android:visible="true" 
        android:enabled="true" 
        android:orderInCategory="2" 
        android:showAsAction="always">

    </item>
    <item android:id="@+id/star" 
        android:icon="@drawable/ic_action_important" 
        android:enabled="true" 
        android:orderInCategory="0" 
        android:showAsAction="always" 
        android:title="Star" 
        android:visible="true">
    </item>

</menu>

下面是我的code

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // TODO Auto-generated method stub
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.general, menu);
    getSupportActionBar().setDisplayShowHomeEnabled(false);
    getSupportActionBar().setDisplayShowTitleEnabled(false);
    getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#003f84")));
    return true;
}

现在,我的问题是菜单项未显示在动作条。难道我做错了什么吗?

Now, my problem is menu items isn't showing in actionbar. Am I doing anything wrong here?

推荐答案

showAsAction 应该是在一个不同的命名空间(<$ C C $> yourapp <​​/ code>样品中的下面)。

showAsAction should be in a different namespace (yourapp in the sample below).

<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
    <item android:id="@+id/action_search"
          android:icon="@drawable/ic_action_search"
          android:title="@string/action_search"
          yourapp:showAsAction="ifRoom"  />
    ...
</menu>

youapp <​​/ code>只是一个指向命名空间的命名空间标识 http://schemas.android.com/apk/res-auto ,你可以把它改成什么。该SDK将命名空间自动映射到你的包名称(请参见下面的changelog)。

youapp is simply a namespace identifier that points to namespace http://schemas.android.com/apk/res-auto, you can change it to anything. The SDK will automatically map that namespace to your package name (see changelog below).

增加了对在库的自定义属性自定义视图的支持。   使用自定义属性布局必须使用命名空间URI    http://schemas.android.com/apk/res-auto 代替的URI   包括应用程序包的名称。该URI替换为应用程序   特定的一个在编译的时候。

Added support for custom views with custom attributes in libraries. Layouts using custom attributes must use the namespace URI http://schemas.android.com/apk/res-auto instead of the URI that includes the app package name. This URI is replaced with the app specific one at build time.

这是必要的,因此,它可以准确地找到不在previous OS版本,而是为你的应用程序包的一部分属性。请注意,从<一个href="http://developer.android.com/guide/topics/ui/actionbar.html#ActionItems">documentation:

This is necessary so that it could properly find attributes that are not available in previous OS versions and instead are part of your application's package. Note from documentation:

请注意,showAsAction上述属性使用自定义命名空间   在标签规定。使用任何XML时,这是必要的   属性的支持库定义的,因为这些属性做   在对旧设备的Andr​​oid框架不存在。所以,你必须使用   你自己的命名空间为preFIX由定义的所有属性   支持库。

Notice that the showAsAction attribute above uses a custom namespace defined in the tag. This is necessary when using any XML attributes defined by the support library, because these attributes do not exist in the Android framework on older devices. So you must use your own namespace as a prefix for all attributes defined by the support library.

这篇关于ActionBarCompat菜单项未显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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