简体   繁体   中英

Popup menu goes out of the screen

Popup menu on inflate goes out of the screen for this activity, works fine for other

在此处输入图像描述

Extending custom AppCompatActivity

Styles:

<style name="AppTheme.ActionBar.TransparentContextTranscition" parent="AppTheme.NoActionBar">
    <item name="android:windowActivityTransitions">true</item>
    <item name="android:statusBarColor">@android:color/transparent</item>
    <item name="android:windowTranslucentStatus">true</item>
    <item name="android:windowContentTransitions">true</item>
    <item name="android:windowLightStatusBar">true</item>

    <item name="android:itemBackground">@android:color/white</item>
    <item name="android:textColor">@android:color/black</item>
    <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
    <item name="android:popupMenuStyle">@style/PopupMenu</item>
</style>

<style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
    <item name="color">@android:color/white</item>
</style>

<style name="PopupMenu" parent="@android:style/Widget.PopupMenu">
    <item name="android:popupBackground">@android:color/white</item>
</style>

Java code

holder.contextMenuView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final PopupMenu popup = new PopupMenu(v.getContext(), v);
            popup.inflate(R.menu.song_item_menu_album_inner_layout);
        }
});

Toolbar inflate menu works fine

在此处输入图像描述

Looks like I was using

getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);

my bad. FLAG_LAYOUT_NO_LIMITS, removes limits for the context menu. (That's the reason, the context menu going out of the screen), Hope that helps for someone

A simple yet-customizable Android drop-down menu. It supports Text with/without Icons, Separators, and even fully customized views

https://github.com/shehabic/Droppy

Unfortunately popup menu does not follow the size of the view it is inside and goes off scope. I had the same problem before. And I know it is not the best solution but here it goes:

Create a fake view which is transparent to use as anchor to block the edge from exceeding.

However since it will be floating you cannot place it on the top or bottom side so careful on the height exceeding while trying this.

And by the way do NOT forget to set android:anyDensity="True/False"

Use this in your code:

final PopupMenu popup = new PopupMenu(v.getContext(), v, Gravity.RIGHT);

if it doesn't work then change the anchor view v. because it can set the position of the popup menu as per your requirement.

You can allow the popup menu to overlap with the anchor view.

More here: https://stackoverflow.com/a/29702608

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM