Android Nougat 7.1.1 showAtLocation(…) Gravity not working

我们两清 提交于 2019-12-01 05:47:17

问题


This is related to this question: Android Nougat PopupWindow showAsDropDown(...) Gravity not working

However, when I applied this fix:

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

It doesn't work on Android Nougat 7.1.1. Particularly on Google Pixel and Nexus 6p devices.

Has anybody got a fix for this? Please share. https://code.google.com/p/android/issues/detail?id=231487


回答1:


When I change PopupWindow's height from WindowManager.LayoutParams.MATCH_PARENT to WindowManager.LayoutParams.WRAP_CONTENT, it works on Android 7.1, I don't know the reason, but maybe you can try it.

Also, you need to change your code to:

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



回答2:


I fixed it, my nexus 5 7.1.1 alread has bug. sample code:

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



回答3:


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

Reference https://stackoverflow.com/a/43873648/4776543 fix some bug.



来源:https://stackoverflow.com/questions/41973893/android-nougat-7-1-1-showatlocation-gravity-not-working

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