Android WebView 加载页面后,弹出键盘遮挡了输入框
经过作者一番查找之后,将问题定位到了这里android的一个bug
但是呢,细细一看评论,发现并不是万能胶,还是会出现一些别的问题。比如作者我,使用了这个类之后导致部分html页面跑到屏幕外面了,并且不能拖动回来。
经过一段不懈的评论区查看(我的英语能力真是过关呀),果然有老哥和我的问题一样,并且给出了解决策略。
专业翻译:就是说呀,对代码的这条语句做出来调整,如果不这么做,就会有一些元素跑到屏幕外面了。
我直接进行cv操作,新的代码如下:
public class AndroidBug5497Workaround {
// For more information, see https://issuetracker.google.com/issues/36911528
// To use this class, simply invoke assistActivity() on an Activity that already has its content view set.
public static void assistActivity (Activity activity) {
new AndroidBug5497Workaround(activity);
}
private View mChildOfContent;
private int usableHeightPrevious;
private FrameLayout.LayoutParams frameLayoutParams;
private AndroidBug5497Workaround(Activity activity) {
FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content);
mChildOfContent = content.getChildAt(0);
mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
public void onGlobalLayout() {
possiblyResizeChildOfContent();
}
});
frameLayoutParams = (FrameLayout.LayoutParams) mChildOfContent.getLayoutParams();
}
private void possiblyResizeChildOfContent() {
int usableHeightNow = computeUsableHeight();
if (usableHeightNow != usableHeightPrevious) {
int usableHeightSansKeyboard = mChildOfContent.getRootView().getHeight();
int heightDifference = usableHeightSansKeyboard - usableHeightNow;
if (heightDifference > (usableHeightSansKeyboard/4)) {
// keyboard probably just became visible
frameLayoutParams.height = usableHeightSansKeyboard - heightDifference;
} else {
// keyboard probably just became hidden
// frameLayoutParams.height = usableHeightSansKeyboard;
frameLayoutParams.height = usableHeightNow;
}
// frameLayoutParams.height = usableHeightNow;
mChildOfContent.requestLayout();
usableHeightPrevious = usableHeightNow;
}
}
private int computeUsableHeight() {
Rect r = new Rect();
mChildOfContent.getWindowVisibleDisplayFrame(r);
return (r.bottom - r.top);
// return (r.bottom);
}
}
之后,在你的activity加载的时候:
AndroidBug5497Workaround.assistActivity(this);
这么来一下,就OK了。
当然这只是解决了我的问题(组件超出屏幕),具体问题还是得具体对待,感谢两位外国老哥和辛勤的解决方案搬运者们(我也是其中之一了,哈哈)。