actionlayout on menuitem does nothing

你。 提交于 2019-11-26 10:56:57

问题


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:

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

my submit_action is:

<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


回答1:


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

If you're using ActionbarSherlock or 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.




回答2:


when using Appcompact ,menu item will be like

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




回答3:


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.




回答4:


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

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


来源:https://stackoverflow.com/questions/23211761/actionlayout-on-menuitem-does-nothing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!