Android Nougat 7.1.1 showAtLocation(...)Gravity无法正常工作

时间:2017-02-01 07:10:19

标签: android android-popupwindow android-7.1-nougat

这与此问题有关: 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

3 个答案:

答案 0 :(得分:4)

当我将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);
}

答案 1 :(得分:0)

我修好了,我的nexus 5 7.1.1 alread有bug。 示例代码:

    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);    

答案 2 :(得分:0)

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修复了一些错误。