问题说明:
在使用53客服内嵌页面时,点击输入框发现软键盘遮住了输入框,很影响体验
问题原因:
webview存在的bug,需要动态的计算webview的高度才可以。
记录原因:
网上也有类似的代码,但是只是解决了虚拟键盘遮挡的问题,如果遇到有虚拟导航栏的话,还是存在遮挡问题,这里总结的方法两种问题都已解决。
正解方法:
加入如下代码:
import android.app.Activity;
import android.graphics.Rect;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.FrameLayout;
public class AndroidBug5497Workaround {
private View mChildOfContent;
private int usableHeightPrevious;
private FrameLayout.LayoutParams frameLayoutParams;
public static void assistActivity (Activity activity) {
new AndroidBug5497Workaround(activity);
}
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 = computeUsableHeight(); //计算可用高度,解决虚拟导航遮挡问题
}
mChildOfContent.requestLayout();
usableHeightPrevious = usableHeightNow;
}
}
private int computeUsableHeight() {
Rect r = new Rect();
mChildOfContent.getWindowVisibleDisplayFrame(r);
return r.bottom;// 全屏模式下: return r.bottom,非全屏:return r.bottom-r.top
}
使用
setContentView(R.layout.layout_web);
AndroidBug5497Workaround.assistActivity(this);
网上也有类似的方案,仅在此记录一下。
frameLayoutParams.height = computeUsableHeight(); //计算可用高度,解决虚拟导航遮挡问题








网友评论