3

这与这个问题有关: Android Nougat PopupWindow showAsDropDown(...) Gravity not working

但是,当我应用此修复程序时:

if (android.os.Build.VERSION.SDK_INT >=24) {
    int[] a = new int[2];
    anchorView.getLocationInWindow(a);
    popUp.showAtLocation(((Activity) mContext).getWindow().getDecorView(), Gravity.NO_GRAVITY, 0 , a[1]+anchorView.getHeight());
} else{
    popUp.showAsDropDown(anchorView);
}

它不适用于 Android Nougat 7.1.1。特别是在 Google Pixel 和 Nexus 6p 设备上。

有没有人解决这个问题?请分享。 https://code.google.com/p/android/issues/detail?id=231487

4

3 回答 3

6

当我将 PopupWindow 的高度从 更改WindowManager.LayoutParams.MATCH_PARENT为 时WindowManager.LayoutParams.WRAP_CONTENT,它适用于 Android 7.1,我不知道原因,但也许您可以尝试一下。

此外,您需要将代码更改为:

if (android.os.Build.VERSION.SDK_INT == 24) {
    int[] a = new int[2];
    anchorView.getLocationInWindow(a);
    popUp.showAtLocation(((Activity)mContext).getWindow().getDecorView(), Gravity.NO_GRAVITY, 0 , a[1]+anchorView.getHeight());
} else{
    popUp.showAsDropDown(anchorView);
}
于 2017-02-07T08:12:40.913 回答
1
public void showFilterWindow(Context context, PopupWindow popupWindow,View showView, int xoff, int yoff) {
    if (Build.VERSION.SDK_INT < 24) {
        //7.0 The following system is used normally
        popupWindow.showAsDropDown(showView, xoff, yoff);
    } else {
        int[] location = new int[2];
        showView.getLocationOnScreen(location);
        int offsetY = location[1] + showView.getHeight() + yoff;
        if (Build.VERSION.SDK_INT == 25) {
            //【note!】Gets the screen height without the virtual key
            WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            int screenHeight = wm.getDefaultDisplay().getHeight();
            /*
             * PopupWindow height for match_parent,
             * will occupy the entire screen, it needs to do special treatment in Android 7.1
            */
            popupWindow.setHeight(screenHeight - offsetY);
        }
        //Use showAtLocation to display pop-up windows
        popupWindow.showAtLocation(showView, Gravity.NO_GRAVITY, 0, offsetY);
    }
}

参考https://stackoverflow.com/a/43873648/4776543修复一些错误。

于 2017-09-22T02:28:58.263 回答
0

我修复了它,我的 nexus 5 7.1.1 已经有错误。示例代码:

    View rootView = anchor.getRootView();
    Rect rect = new Rect();
    rootView.getWindowVisibleDisplayFrame(rect);

    int[] xy = new int[2];
    anchor.getLocationInWindow(xy);
    int anchorY = xy[1] + anchor.getHeight();

    int height = rect.bottom - anchorY;

    PopupWindow poupWindow = new PopupWindow(frameLayout, ViewGroup.LayoutParams.MATCH_PARENT, height);
    poupWindow.showAtLocation(anchor, Gravity.NO_GRAVITY, 0, anchorY);    
于 2017-04-14T11:19:12.507 回答