Can I use a actionLayout on the overflow menu of android.support.v7.widget.Toolbar?

佐手、 提交于 2019-12-01 03:14:20

It's not possible.

That's because actionLayout is used only when the item is shown as action. It's equivalent to setActionView and, from the documentation:

Set an action view for this menu item. An action view will be displayed in place of an automatically generated menu item element in the UI when this item is shown as an action within a parent.

So, you can inflate a custom view if it's used as an action (if showAsAction is never, then it's not shown as action, as expected).

Now, hidden somewhere else, in the documentation for ActionProvider, there's this:

When the menu item is presented in a way that does not allow custom action views, (e.g. in an overflow menu,) the ActionProvider can perform a default action.

It's explicitly stated that overflow menu can't display custom view.

[Edit: possible workaround]
If you really want to display it that way, you can fake an overflow menu with PopupWindow, but then you may lose consistency with the platform in some devices or even after an upgrade to Android (let's say… you used three round dots to simulate the overflow menu, but later it is changed back to squares on Android, your app will look odd. There's also the extra effort to make it work while with a Menu it would be straightforward.

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