Android 全屏情况下 webview中输入框被软件盘遮挡问题解决方案

释放双眼,带上耳机,听听看~!

问题是这样的,webview中加载的HTML页面含有input标签,不隐藏状态栏和导航栏的情况下,软件盘能正常将布局顶起。当设置Activity全屏后,软件盘无法顶起布局。

解决方法:

1.新建一个类(类名随意),我这里命名为AndroidBug5497Workaround,实现代码如下:

/**
* Created by SkySmile on 2017/9/6.
* <p>
* For more information, see https://code.google.com/p/android/issues/detail?id=5497
* To use this class, simply invoke assistActivity() on an Activity that already has its content view set.
*/
public class AndroidBug5497Workaround {
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(() -> 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;
}
mChildOfContent.requestLayout();
usableHeightPrevious = usableHeightNow;
}
}
private int computeUsableHeight() {
Rect r = new Rect();
mChildOfContent.getWindowVisibleDisplayFrame(r);
return (r.bottom - r.top);
}
}

这个类我是从网上找的 ^_^

2.在Activity中使用(我是在onCreate方法中调用的)。

AndroidBug5497Workaround.assistActivity(this);

人已赞赏
Android文章

Android开发关于getHeight与getMeasureHeight的问题

2020-3-6 19:03:39

Android文章

Android手机底部导航栏遮挡住布局问题解决方案

2020-3-7 10:44:40

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
有新消息 消息中心
搜索