如果底部 space 不足,如何更改 showAsDropdown 弹出窗口 window 的位置?

How to change position of showAsDropdown popup window if there is not enough space in the bottom?

我正在使用 recyclerview,它的每个项目都有一个菜单。我使用弹出 window 来实现相同的效果,而不是弹出 menu 因为我有自定义布局。我面临的问题是,当我从屏幕底部的项目打开菜单时,我的菜单不完全可见并且被剪切有没有办法解决它?

这是我的菜单(有 4 个选项)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/menuItemLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/shadow_menu"
    android:gravity="center"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/white"
        android:orientation="vertical">

    <LinearLayout
        android:id="@+id/menuAttendeeMeet"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?android:attr/selectableItemBackground"
        android:clickable="true"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:layout_marginTop="9dp"
        android:paddingBottom="12dp"
        android:paddingTop="12dp">

        <ImageView
            android:id="@+id/ivattendee_meet"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:src="@drawable/meet_icon" />

        <TextView
            android:id="@+id/tvattendee_meet"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Meet"
            android:layout_marginRight="16dp"
            android:textColor="@color/textPrimary"
            android:textSize="16dp" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/menuAttendeeChat"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?android:attr/selectableItemBackground"
        android:clickable="true"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:paddingBottom="12dp"
        android:paddingTop="12dp">

        <ImageView
            android:id="@+id/ivattendee_chat"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:src="@drawable/chat_icon" />

        <TextView
            android:id="@+id/tvattendee_chat"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Chat"
            android:layout_marginRight="16dp"
            android:textColor="@color/textPrimary"
            android:textSize="16dp" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/menuAttendeeTakeANote"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?android:attr/selectableItemBackground"
        android:clickable="true"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:paddingBottom="12dp"
        android:paddingTop="12dp">

        <ImageView
            android:id="@+id/ivattendee_note"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:src="@drawable/take_a_note_icon" />

        <TextView
            android:id="@+id/tvattendee_note"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Take a note"
            android:layout_marginRight="16dp"
            android:textColor="@color/textPrimary"
            android:textSize="16dp" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/menuAttendeeBusinessCard"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?android:attr/selectableItemBackground"
        android:clickable="true"
        android:gravity="center_vertical"
        android:layout_marginBottom="9dp"
        android:orientation="horizontal"
        android:paddingBottom="12dp"
        android:paddingTop="12dp">

        <ImageView
            android:id="@+id/ivattendee_business_card"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:src="@drawable/business_card_icon" />

        <TextView
            android:id="@+id/tvattendee_business_card"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Business Card"
            android:layout_marginRight="16dp"
            android:textColor="@color/textPrimary"
            android:textSize="16dp" />
    </LinearLayout>
    </LinearLayout>
</LinearLayout>

这是我在适配器内弹出 window:

final PopupWindow popupWindow = new PopupWindow(v.getContext());
LayoutInflater inflater = (LayoutInflater) v.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View view = inflater.inflate(R.layout.layout_pop_up_menu_attendee, null);
popupWindow.setFocusable(true);
popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
popupWindow.setOutsideTouchable(true);
popupWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
popupWindow.setContentView(view);

int ivHeight = holder.ivAttendeeMenu.getHeight() - 20;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    popupWindow.showAsDropDown(holder.ivAttendeeMenu, 0, -ivHeight, Gravity.END);
}
View container;
if (android.os.Build.VERSION.SDK_INT > 22) {
    container = (View) popupWindow.getContentView().getParent().getParent();
} else {
    container = (View) popupWindow.getContentView().getParent();
}

WindowManager wm = (WindowManager) v.getContext().getSystemService(Context.WINDOW_SERVICE);
WindowManager.LayoutParams p = (WindowManager.LayoutParams) container.getLayoutParams();

p.flags |= WindowManager.LayoutParams.FLAG_DIM_BEHIND;
p.dimAmount = 0.3f;
wm.updateViewLayout(container, p);

如果没有足够的space显示整个菜单,您可以将菜单打开到顶部并使用以下代码

            final PopupWindow popupWindow = new PopupWindow(v.getContext());
            LayoutInflater inflater = (LayoutInflater) v.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            View view = inflater.inflate(R.layout.layout_pop_up_menu_attendee, null);
            popupWindow.setFocusable(true);
            popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            popupWindow.setOutsideTouchable(true);
            popupWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
            popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
            popupWindow.setContentView(view);

            //First we get the position of the menu icon in the screen 
            int[] values = new int[2];
            holder.ivAttendeeMenu.getLocationInWindow(values);
            int positionOfIcon = values[1];
            System.out.println("Position Y:" + positionOfIcon);

            //Get the height of 2/3rd of the height of the screen
            DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
            int height = (displayMetrics.heightPixels * 2) / 3;
            System.out.println("Height:" + height);

            //If the position of menu icon is in the bottom 2/3rd part of the screen then we provide menu height as offset  but in negative as we want to open our menu to the top
            if (positionOfIcon > height) {
                popupWindow.showAsDropDown(holder.ivAttendeeMenu, 0, -320);
            } else {
                popupWindow.showAsDropDown(holder.ivAttendeeMenu, 0, 0);
            }