Android Nougat PopupWindow showAsDropDown(...)Gravity无法正常工作

问题描述 投票:13回答:5

我有这个代码。

PopupWindow popUp = new PopupWindow();
popUp.setFocusable(true);
popUp.setOutsideTouchable(true);        
popUp.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
popUp.setHeight(600);

popUp.setContentView(anchorView);
popUp.showAsDropDown(anchorView);
popUp.update();

它完美适用于Android版<Android Nougat。但是在Android Nougat中,弹出窗口显示在屏幕顶部而不是相对于锚点视图。

android popupwindow android-7.0-nougat
5个回答
16
投票

这似乎是android 7.0中的一个bug。但你可以用兼容的方式解决它。

PopupWindow popUp = new PopupWindow();
popUp.setFocusable(true);
popUp.setOutsideTouchable(true);        
popUp.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
popUp.setHeight(600);

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

popUp.update();

谷歌将在未来的构建中修复此错误。还有最后的解决方法。你需要在创建pop时给出高度。

PopupWindow popup = new PopupWindow(contentView, with, height);

如上弹出初始化,你只能使用popUp.showAsDropDown(anchorView)显示这个弹出窗口。通过这种方式,您可以忽略Android API的版本。


5
投票

7.0和7.1实现不同,所以要分开处理。

我在虚拟机(7.0和7.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);
        }
    }

2
投票

看起来此问题仅出现在Android 7.0(API 24)中。在7.1.1(API 25)中,在同一设备上一切正常。经过一些研究确定popUp.update()引起的问题,就像它已经被Marilia提到的那样。但是如果您只是删除popUp.update(),弹出窗口将不会出现在API 24之前的版本中。为了避免这种情况,现在唯一的方法是使用版本检查,不要仅在具有API 24的设备上使用update()方法。这是解决方案,对我有用:

if (Build.VERSION.SDK_INT != 24) {
   popup.update(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
}

在不同的设备和API上进行了测试,从API 18到API 25,它都能很好地运行。


1
投票

你真的需要在你的代码中使用popUp.update();吗?我有一个类似的问题,在我的情况下,我不需要popUp.update();并删除它使弹出重力表现如预期。

此外,这很可能是一个相关的问题,报道有关PopupWindow.showAtLocation()

https://code.google.com/p/android/issues/detail?id=221001


0
投票

这段代码对我有用。请试一试

    protected void showSortPopup(View anchorView) {


    if (Build.VERSION.SDK_INT >= 25) {
        Rect rectf = new Rect();
        anchorView.getGlobalVisibleRect(rectf);
        int offsetY = (rectf.top + anchorView.getHeight());
        WindowManager wm = (WindowManager) Manager.getInstance().getCurrentActivity().getSystemService(Context.WINDOW_SERVICE);
        int screenHeight = wm.getDefaultDisplay().getHeight();
        mPopup.setHeight(screenHeight - offsetY);
    }
    mPopup.showAsDropDown(anchorView);

}
© www.soinside.com 2019 - 2024. All rights reserved.