溢出按钮上方的锚点弹出菜单 [英] anchor popup menu over overflow button
问题描述
我正在尝试创建一个自定义弹出菜单,我想将其放置在操作栏的溢出菜单按钮上(我正在使用带有 setSupportActionBar()
的 Toolbar
为此,如果重要的话).我发现这可以通过 setAnchorView()
完成(来自阅读 溢出菜单中的自定义菜单项).
I'm trying to create a custom popup menu that I would like to position on the overflow menu button of the actionbar (am using a Toolbar
with setSupportActionBar()
for this if it matters). I found out that this can be done with the setAnchorView()
(from reading Custom Menu item in Overflow menu).
但是我似乎无法弄清楚如何将溢出菜单检索为视图(我可以用来设置锚点).
However I cant seem to figure out how to retrieve the overflow menu as a view (that I could use to set the anchor with).
我也尝试将其锚定到活动本身的父布局,但它显示在左上角,并且菜单的高度等于操作栏的高度(这不是很有用).
Also I did try anchoring this to the parent layout of the activity itself but it showed up on the left top, and the height of the menu was equal to the height of the actionbar (which is not very useful).
有谁知道这是如何实现的?
Does anyone know how this can be achieved?
推荐答案
您可以稍微欺骗"一下,而不是拥有真正的溢出菜单.在您的操作栏中有一个看起来像溢出图标的图标.您应该将 showAsAction
设置为始终在此 MenuItem
上.OnClick 溢出图标,你会显示一个 ListPopupWindow
锚定到 MenuItem
视图.如果 ListPopupWindow
没有出现在你想要的地方,你可以调用 ListPopupWindow.setHorizontalOffset()
和 ListPopupWindow.setVerticalOffset()
Instead of having an actual overflow menu you could "cheat" a little bit. Have an icon in your actionbar that looks like the overflow icon. You should set showAsAction
to always on this MenuItem
. OnClick of the overflow icon, you show a ListPopupWindow
that's anchored to the MenuItem
view. If the ListPopupWindow
doesn't show up where you want it to, you can call ListPopupWindow.setHorizontalOffset()
and ListPopupWindow.setVerticalOffset()
这篇关于溢出按钮上方的锚点弹出菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!