【问题标题】:Android Nougat 7.1.1 showAtLocation(...) Gravity not workingAndroid Nougat 7.1.1 showAtLocation(...) 重力不起作用
【发布时间】:2017-02-01 07:10:18
【问题描述】:

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

【问题讨论】:

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


【解决方案1】:

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

【讨论】:

  • 这对我们不起作用。我的设备是 Nexus 5x Android 7.1.1
【解决方案2】:
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修复一些bug。

【讨论】:

    【解决方案3】:

    我已修复它,我的 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-02-02
      • 1970-01-01
      • 1970-01-01
      • 2019-01-14
      • 1970-01-01
      • 1970-01-01
      • 2021-12-30
      • 2013-09-01
      • 1970-01-01
      相关资源
      最近更新 更多