在溢出按钮锚弹出菜单弹出、按钮、菜单

2023-09-12 23:39:46 作者:梦港

我想创建一个自定义弹出菜单,我想(我使用的是工具栏与 setSupportActionBar()此,如果它事项)。我发现,这可能与 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 来一直在这个菜单项。溢图标的OnClick,你表现出 ListPopupWindow 多数民众赞成固定在菜单项视图。如果你想要它,你可以调用 ListPopupWindow.setHorizo​​ntalOffset() ListPopupWindow 显示不出来 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()