简体   繁体   English

Android appcompat操作栏菜单项showAsAction无效

[英]Android appcompat actionbar menu item showAsAction not working

I have a menu item that is showing up on android 4.x but not on 2.x. 我有一个菜单项在android 4.x上显示但不在2.x上显示。 Here is my menu.xml 这是我的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/menu_filter"
    android:title="Filter"
    app:showAsAction="always"/>  
</menu>

This is my actionbar style 这是我的动作栏样式

<style name="style1_actionBar" parent="@style/Widget.AppCompat.Light.ActionBar">
    <item name="android:background">@color/blue_dark</item>
    <item name="android:textColor">@color/white</item>
    <item name="actionMenuTextAppearance">@color/white</item>
    <item name="background">@color/blue_dark</item>
</style>

Any ideas? 有任何想法吗?

Edit: removed double quote typo 编辑:删除双引号拼写错误

Could it be the fact that I am showing only text, no icons? 可能是因为我只显示文字,没有图标吗? I'm kind of stuck here. 我有点被困在这里。

Whew, thanks for your help guys but I managed to figure it out. 哎呀,谢谢你的帮助,但我设法搞清楚了。 It wasn't a problem with the xml, it was a problem with the onCreateOptionsMenu function. 它不是xml的问题,它是onCreateOptionsMenu函数的一个问题。

I was using this 我正在使用它

new MenuInflater(getApplication()).inflate(R.menu.activity_wentry_editor, menu); 

instead of this 而不是这个

MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_wentry_editor, menu);

Not entirely sure why this works but it does. 不完全确定为什么会这样,但确实如此。

<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>

Please refer to the documentation. 请参阅文档。 http://developer.android.com/guide/topics/ui/actionbar.html http://developer.android.com/guide/topics/ui/actionbar.html

Using XML attributes from the support library 使用支持库中的XML属性

Notice that the showAsAction attribute above uses a custom namespace defined in the tag. 请注意,上面的showAsAction属性使用标记中定义的自定义命名空间。 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. 在使用支持库定义的任何XML属性时,这是必需的,因为旧设备上的Android框架中不存在这些属性。 So you must use your own namespace as a prefix for all attributes defined by the support library. 因此,您必须使用自己的命名空间作为支持库定义的所有属性的前缀。

In my case I had to add a few lines to onCreateOptionsMenu. 在我的情况下,我不得不在onCreateOptionsMenu上添加几行。

Android Studio didn't let me use android:showAsAction="ifRoom" while using appCompat. 在使用appCompat时,Android Studio没有让我使用android:showAsAction =“ifRoom”。

app:showAsAction="ifRoom" wasn't working and I removed it without problems. app:showAsAction =“ifRoom”无法正常工作,我将其删除没有问题。

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater  inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        for (int i = 0; i < menu.size(); i++) {
            menu.getItem(i).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
        }
        return super.onCreateOptionsMenu(menu);
    }

If you want your app to support action bar below 3.0 you need to use app compact v7 from the support library. 如果您希望您的应用支持低于3.0的操作栏,则需要使用支持库中的app compact v7。

Also check the link 另请查看链接

Using the menu in an activity that extends the AppCompact it is necessary import the app context in the XML and use it: 使用扩展AppCompact的活动中的菜单,必须在XML中导入应用程序上下文并使用它:

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

    <!-- "Mark Favorite", should appear as action button if possible -->
    <item
        android:id="@+id/action_favorite"
        android:icon="@drawable/ic_favorite_black_48dp"
        android:title="@string/action_favorite"
        app:showAsAction="ifRoom"/>

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

</menu>

What you need to do basically is add xmlns:app="http://schemas.android.com/apk/res-auto" to the menu element in yout XML and use the showAsAction in the following format: app:showAsAction="ifRoom" . 您需要做的基本上是将xmlns:app="http://schemas.android.com/apk/res-auto"到yout XML中的menu元素中,并使用以下格式的showAsActionapp:showAsAction="ifRoom"

This will show the icon in the action bar, if possible. 如果可能,这将在操作栏中显示图标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM