直接复制代码:
Kotlin:
Kotlin
mWebView?.getViewTreeObserver()?.addOnGlobalLayoutListener {
val r = Rect()
mWebView?.getWindowVisibleDisplayFrame(r)
val screenHeight = mWebView?.getRootView()?.height ?: 0
val keypadHeight = screenHeight - r.bottom
val layoutParams = mWebView?.layoutParams
if (keypadHeight > screenHeight * 0.15) { // 如果键盘高度超过屏幕高度的15%则认为键盘显示
layoutParams?.height = screenHeight - keypadHeight; // 调整WebView高度
mWebView?.layoutParams = layoutParams
} else if (layoutParams?.height != FrameLayout.LayoutParams.MATCH_PARENT) { // 如果键盘隐藏,恢复原始高度或MATCH_PARENT
layoutParams?.height = FrameLayout.LayoutParams.MATCH_PARENT; // 或者设置为某个固定高度
mWebView?.layoutParams = layoutParams
}
}
Java:
java
yourWebView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Rect r = new Rect();
yourWebView.getWindowVisibleDisplayFrame(r);
int screenHeight = yourWebView.getRootView().getHeight();
int keypadHeight = screenHeight - r.bottom;
FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams)yourWebView.getLayoutParams();
if (keypadHeight > screenHeight * 0.15) { // 如果键盘高度超过屏幕高度的15%则认为键盘显示
layoutParams.height = screenHeight - keypadHeight; // 调整WebView高度
yourWebView.setLayoutParams(layoutParams);
} else if (layoutParams.height != FrameLayout.LayoutParams.MATCH_PARENT) { // 如果键盘隐藏,恢复原始高度或MATCH_PARENT
layoutParams.height = FrameLayout.LayoutParams.MATCH_PARENT; // 或者设置为某个固定高度
yourWebView.setLayoutParams(layoutParams);
}
}
});
更多算法内容推荐:
Android 相关文章
https://blog.csdn.net/qq_39731011/category_7565212.html
如果您感觉文章有用的话,麻烦点个赞吧!
如果您发现文章有任何错误或建议,请评论区留言或者私信!
深海谢过各位的支持,一起加油!
