android 7.0 上PopupWindow showAsDropDown update


1.  7.0上PopupWindow 调用update、showAsDropDown方法导致PopupWindow 设置的宽高无效,showAsDropDown无效,显示在屏幕左上角; 

https://blog.csdn.net/m190607070/article/details/58618662


适配
public void showAsDownView(View view){


        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {//在7.0之前
            popupWindow.showAsDropDown(view, 0, 0);
        } else {
        // 适配 android 7.0
            int[] location = new int[2];
            view.getLocationOnScreen(location);
            int x = location[0];
            int y = location[1];
            popupWindow.showAtLocation(view, Gravity.NO_GRAVITY, 0,y);
        }
    }


public  void showAsDownView(View anchor, int xoff, int yoff) {
        
	//7.0
        if (Build.VERSION.SDK_INT >= 24) {
            int[] location = new int[2];
            anchor.getLocationOnScreen(location);

            // 7.1 修复
            if (Build.VERSION.SDK_INT == 25) {
                //【note!】Gets the screen height without the virtual key
                WindowManager wm = (WindowManager) this.getContentView().getContext().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
                */
                this.setHeight(screenHeight - location[1] - anchor.getHeight() - yoff);
            }
            this.showAtLocation(anchor, Gravity.NO_GRAVITY, xoff, location[1] + anchor.getHeight() + yoff);
        } else {
            this.showAsDropDown(anchor, xoff, yoff);
        }
    }


2.

我在在项目动态调用update方法去实现更新它的宽高不起作用,https://blog.csdn.net/u011449334/article/details/54318092


代码如下:  


update(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);

但是总是不起作用,去查了下update方法,一部分具体如下:
 public void update(int x, int y, int width, int height, boolean force) {
       if (width != -1) {
           mLastWidth = width;
           setWidth(width);
       }
 
       if (height != -1) {
           mLastHeight = height;
           setHeight(height);
       }
 
然而LinearLayout.LayoutParams.MATCH_PARENT的值就是-1.
解决办法就是如果想动态更新popupwindow的宽或高为充满,那么可以把
LinearLayout.LayoutParams.MATCH_PARENT设为获取屏幕的实际宽高去设置。


猜你喜欢

转载自blog.csdn.net/kongbaidepao/article/details/80541699