PopupWindow 全屏问题两种解决方案

点击调用showAsDropDown(view)后,PopupWindow 填充了整个屏幕,如图所示

解决后正常显示,如图2所示

图2

 

解决办法:

  1. 计算PopupWindow要显示的高度   

       

     popupwindow的高度= LinearLayout的高度控件的bottom的高度

    void showOrderItemsPPWindow() {
            final View contentView = LayoutInflater.from(DeliveOrderActivity.this).inflate(R.layout.ppw_order_pick_type, null, false);
    //       popupwindow的高度 = LinearLayout的高度 - 控件的bottom的高度
            int height = deliveLlayout.getHeight() - orderItemBtn.getBottom();
            final PopupWindow window = new PopupWindow(contentView, LinearLayout.LayoutParams.MATCH_PARENT, height);
    
    
    
            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            window.setOutsideTouchable(true);
            window.setTouchable(true);
            window.showAsDropDown(orderItemBtn, 0, 0, Gravity.BOTTOM);
            window.setBackgroundDrawable(new ColorDrawable(Color.BLACK));
    
            LinearLayout linearLayout = contentView.findViewById(R.id.full_llayout);
            linearLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    window.dismiss();
                }
            });
            window.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {
    
                }
            });
        }

     

  2.  

    重写Popupwindow的showAsDropDown方法

    public class MPopupWindow extends PopupWindow {
    
    
        public MPopupWindow(View contentView, int width, int height) {
            super(contentView, width, height);
        }
    
        @Override
        public void showAsDropDown(View anchor) {
            if (Build.VERSION.SDK_INT >= 24){
                Rect ew = new Rect();
                anchor.getGlobalVisibleRect(ew);
                int height = anchor.getResources().getDisplayMetrics().heightPixels - ew.top;
                setHeight(height);
            }
    
            super.showAsDropDown(anchor);
        }
    }

     

        void showOrderItemsPPWindow() {
            final View contentView = LayoutInflater.from(DeliveOrderActivity.this).inflate(R.layout.ppw_order_pick_type, null, false);
            final MPopupWindow window = new MPopupWindow(contentView, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            window.setOutsideTouchable(true);
            window.setTouchable(true);
            window.showAsDropDown(orderItemBtn);
            window.setBackgroundDrawable(new ColorDrawable(Color.BLACK));
    
            LinearLayout linearLayout = contentView.findViewById(R.id.full_llayout);
            linearLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    window.dismiss();
                }
            });
            window.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {
    
                }
            });
        }

     

     

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PopupWindow弹出时,可以通过设置PopupWindow的softInputMode属性来控制软键盘的显示和隐藏。一般情况下,我们可以将PopupWindow的softInputMode属性设置为PopupWindow.INPUT_METHOD_NEEDED,表示PopupWindow需要软键盘,但是PopupWindow并不会自动调整大小以避免遮盖软键盘。 因此,为了解决PopupWindow遮住软键盘的问题,我们可以在PopupWindow弹出时,手动调整PopupWindow的大小,使其避开软键盘。具体实现方法如下: 1. 获取当前软键盘的高度: ```java public int getSoftInputHeight(Activity activity) { Rect rect = new Rect(); activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect); int screenHeight = activity.getWindow().getDecorView().getRootView().getHeight(); int softInputHeight = screenHeight - rect.bottom; if (softInputHeight > 0 && softInputHeight < screenHeight / 3) { return softInputHeight; } return 0; } ``` 2. 在PopupWindow弹出时,获取软键盘高度并调整PopupWindow的大小: ```java mPopupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); // 监听PopupWindow的布局变化 mPopupWindow.getContentView().getViewTreeObserver() .addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { int softInputHeight = getSoftInputHeight(activity); if (softInputHeight > 0) { // 获取PopupWindow的布局参数 ViewGroup.LayoutParams layoutParams = mPopupWindow.getContentView().getLayoutParams(); layoutParams.height = screenHeight - softInputHeight; mPopupWindow.getContentView().setLayoutParams(layoutParams); } } }); ``` 这样,当软键盘弹出时,PopupWindow就会自动调整大小以避开软键盘。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值