android 7.0 上PopupWindow showAsDropDown update

本文针对Android 7.0上PopupWindow显示位置及尺寸问题提供了解决方案,包括showAsDropDown方法的适配及动态调整PopupWindow大小的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


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

https://blog.youkuaiyun.com/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.youkuaiyun.com/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设为获取屏幕的实际宽高去设置。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

李磊LL

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值