Android menuitem 上的 actionlayout 什么都不做

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23211761/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:53:14  来源:igfitidea点击:

actionlayout on menuitem does nothing

androidandroid-actionbarandroid-menu

提问by tipu

i am setting an actionLayout on a menu item and setting background color and image, but it's not respected. in my activity, i have:

我在菜单项上设置 actionLayout 并设置背景颜色和图像,但它不受尊重。在我的活动中,我有:

getMenuInflater().inflate(R.menu.submit_action, menu);

my submit_action is:

我的 submit_action 是:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item android:id="@+id/action_submit"
        android:actionLayout="@layout/check"
        app:showAsAction="always"  />
</menu>

my check layout is

我的支票布局是

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    style="?android:attr/actionButtonStyle"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#e8e8e8"
    android:clickable="true"
    android:contentDescription="lol" >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@null"
        android:scaleType="centerInside"
        android:src="@drawable/ic_action_tick" />

</RelativeLayout>

but even with all of this setup, this is how the action bar appears, not showing my menuitem at all (but it's there, because it responds to the click, but doesn't appear):

但即使有所有这些设置,这就是操作栏的显示方式,根本不显示我的菜单项(但它在那里,因为它响应点击,但没有出现):

enter image description here

在此处输入图片说明

回答by Ben Harris

Try app:actionLayout="@layout/check"instead of android:actionLayout="@layout/check".

尝试app:actionLayout="@layout/check"代替android:actionLayout="@layout/check".

If you're using ActionbarSherlockor AppCompat, the android:namespace will not work for MenuItems. This is because these libraries use custom attributes that mimic the Android APIs since they did not exist in earlier versions of the framework.

如果您使用ActionbarSherlockor AppCompatandroid:命名空间将不适用于MenuItems。这是因为这些库使用模仿 Android API 的自定义属性,因为它们在早期版本的框架中不存在。

回答by Tarun Voora

when using Appcompact ,menu item will be like

使用 Appcompact 时,菜单项将类似于

<item android:id="@+id/cart"
    app:actionLayout="@layout/actionbar_cart"
    android:title="@string/action_cart"
    app:showAsAction="always"
 />

回答by neaGaze

The answer from Ben Harris is absolutely correct. However, in some case like when using attributes like:

本哈里斯的回答是绝对正确的。但是,在某些情况下,例如使用以下属性时:

      app:showAsAction="ifRoom|collapseActionView"

used in SearchView (in my case), the layout view isn't shown and that caused a lot of headache to me. It seems that collapseActionView is not supported with action view in appcombat. So consider this as well while doing your stuffs.

在 SearchView 中使用(在我的情况下),未显示布局视图,这让我很头疼。appcombat中的动作视图似乎不支持collapseActionView。所以在做你的事情时也要考虑这一点。

回答by coding.cat3

use app namespace instead of android and it will work fine.

使用 app 命名空间而不是 android,它会正常工作。

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item android:id="@+id/action_submit"
        app:actionLayout="@layout/check"
        app:showAsAction="always"  />
</menu>