实际项目维护中,华为手机测试过程软键盘弹起后,收起软键盘。页面没有渲染成功。经过长时间检查,终于解决。参考方案
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);
}
}
}
欢迎点赞、收藏、转发。