android实战之软键盘兼容华为手机

实际项目维护中,华为手机测试过程软键盘弹起后,收起软键盘。页面没有渲染成功。经过长时间检查,终于解决。参考方案

try {
            web();
            ViewGroup.LayoutParams params = webView.getLayoutParams();
            if (params instanceof ConstraintLayout.LayoutParams) {
                ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) params;
                decorView = getActivity().getWindow().getDecorView();
                if (decorView != null) {
                    // 设置全局布局监听器
                    globalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
                        @Override
                        public void onGlobalLayout() {
                            if (webView != null && decorView.getViewTreeObserver().isAlive()) {
                                Rect rect = new Rect();
                                decorView.getWindowVisibleDisplayFrame(rect);
                                int screenHeight = decorView.getRootView().getHeight();
                                int keyboardHeight = screenHeight - rect.bottom;
                                // 过滤掉键盘快速弹出和消失的情况(可选)
                                if (Math.abs(currentHeight - keyboardHeight) > 10) {
                                    currentHeight = keyboardHeight;
                                    // 更新webView的布局参数
                                    ConstraintLayout.LayoutParams lp = (ConstraintLayout.LayoutParams) webView.getLayoutParams();
                                    if (lp != null) {
                                        lp.bottomMargin = keyboardHeight;
                                        webView.setLayoutParams(lp);
                                    }
                                }
                            }
                        }
                    };
                    ViewTreeObserver viewTreeObserver = decorView.getViewTreeObserver();
                    if (viewTreeObserver.isAlive() && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                        viewTreeObserver.addOnGlobalLayoutListener(globalLayoutListener);
                    }
                }
            }

欢迎点赞、收藏、转发。

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值