How to force PopupMenu to overlap anchor?

南笙酒味 提交于 2019-12-04 20:39:33

问题


How to force PopupMenu to overlap anchor? I would like to recreate something similar to this:


回答1:


Setting overlapAnchor allowed me to overlap anchor view without setting offset:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="popupMenuStyle">@style/PopupMenu</item>
</style>

<style name="PopupMenu" parent="@style/Widget.AppCompat.Light.PopupMenu">
    <item name="overlapAnchor">true</item>
</style>

The PopupMenu wraps a ListPopupWindow inside it. This ListPopupWindow wraps AppCompatPopupWindow using this overlapAnchor attribute. All this in the support library.




回答2:


Alternative to Paritosh Tonk's answer:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="popupMenuStyle">@style/Widget.AppCompat.Light.PopupMenu.Overflow</item>
</style>

or for dark theme:

<style name="AppTheme.Dark" parent="Theme.AppCompat.NoActionBar">
    <item name="popupMenuStyle">@style/Widget.AppCompat.PopupMenu.Overflow</item>
</style>



回答3:


Setting android:dropDownVerticalOffset allowed me to overlap anchor view:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="popupMenuStyle">@style/PopupMenu</item>
</style>

<style name="PopupMenu" parent="@style/Widget.AppCompat.Light.PopupMenu">
    <item name="android:dropDownVerticalOffset">-36dip</item>
</style>



回答4:


Have you looked at using PopupWindow instead? Those offer a bit more flexibility in their placement. You can specify the anchor view and an x and y offset which would let you define the overlap.




回答5:


I came here when trying to figure out how to create a popup menu that overlaps its anchor when the anchor is in the actionBar. Since this is a prominent search result for that problem, I want to leave the answer here even though I'm not sure whether the solution would work for OP.

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto" >
    <!-- This item will expand into a submenu -->
    <item
        android:id="@+id/anchor"
        android:icon="@drawable/ic_anchor"
        android:title="@string/anchor"
        app:showAsAction="always">
        <menu>
            <item
                android:id="@+id/subMenuItemOne"
                android:showAsAction="never"
                android:title="SubMenuItemOne"/>
            <item
                android:id="@+id/subMenuItemTwo"
                android:showAsAction="never"
                android:title="SubMenuItemTwo"/>
        </menu>
    </item>
</menu>


来源:https://stackoverflow.com/questions/28202380/how-to-force-popupmenu-to-overlap-anchor

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