Skip to content Skip to sidebar Skip to footer

Android Nougat 7.1.1 Showatlocation(...) Gravity Not Working

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)

Solution 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 = newint[2];
    anchorView.getLocationInWindow(a);
    popUp.showAtLocation(((Activity)mContext).getWindow().getDecorView(), Gravity.NO_GRAVITY, 0 , a[1]+anchorView.getHeight());
} else{
    popUp.showAsDropDown(anchorView);
}

Solution 2:

publicvoidshowFilterWindow(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 = newint[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.

Solution 3:

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

ViewrootView= anchor.getRootView();
    Rectrect=newRect();
    rootView.getWindowVisibleDisplayFrame(rect);

    int[] xy = newint[2];
    anchor.getLocationInWindow(xy);
    intanchorY= xy[1] + anchor.getHeight();

    intheight= rect.bottom - anchorY;

    PopupWindowpoupWindow=newPopupWindow(frameLayout, ViewGroup.LayoutParams.MATCH_PARENT, height);
    poupWindow.showAtLocation(anchor, Gravity.NO_GRAVITY, 0, anchorY);    

Post a Comment for "Android Nougat 7.1.1 Showatlocation(...) Gravity Not Working"