1.虚拟键盘弹出时WebView输入框被挡住 2.底部虚拟菜单栏弹出会挡住界面

Fragment中使用WebView,软键盘弹出时挡住WebView的输入框

1.虚拟键盘弹出时WebView输入框被挡住 2.底部虚拟菜单栏弹出会挡住界面_第1张图片
1.虚拟键盘弹出时WebView输入框被挡住 2.底部虚拟菜单栏弹出会挡住界面_第2张图片

在华为手机上,底部虚拟键弹出会挡住界面

1.虚拟键盘弹出时WebView输入框被挡住 2.底部虚拟菜单栏弹出会挡住界面_第3张图片

解决方法,代码如下:

public class AndroidBug5497Workaround {

    public static int mTabHeight = 250;//主布局中Table的高度
    private Activity activity;

    public static void assistActivity(Activity activity) {
        new AndroidBug5497Workaround(activity);
    }

    private View mChildOfContent;
    private int usableHeightPrevious;
    private FrameLayout.LayoutParams frameLayoutParams;

    private AndroidBug5497Workaround(Activity activity) {
        this.activity = activity;
        FrameLayout content = 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)) {
                // 虚拟键盘弹出时挡住WebView中的输入框
                frameLayoutParams.height = usableHeightNow + 100 + mTabHeight;
            } else {
                int h_phone = AppUtils.getPhoneH(activity);
                int h_bottom = AppUtils.getBottomBarHeight(activity);
                if (h_phone - usableHeightNow == h_bottom) {
                    //华为手机底部菜单栏显示
                    frameLayoutParams.height = usableHeightSansKeyboard - h_bottom;
                } else {
                    frameLayoutParams.height = usableHeightSansKeyboard;
                }
            }
            mChildOfContent.requestLayout();
            usableHeightPrevious = usableHeightNow + 100 + mTabHeight;
        }
    }

    private int computeUsableHeight() {
        Rect r = new Rect();
        mChildOfContent.getWindowVisibleDisplayFrame(r);
        return (r.bottom - r.top);
    }
}

其它代码如下:

  //去掉顶部状态栏的屏幕真实高度
    public static int getPhoneH(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getRealMetrics(dm); //获取真正的屏幕分辨率
        int result = 0;
        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = context.getResources().getDimensionPixelSize(resourceId);
        }
        return dm.heightPixels - result;
    }

    //底部菜单栏的高度
    public static int getBottomBarHeight(Context context) {
        int resourceId = 0;
        int rid = context.getResources().getIdentifier("config_showNavigationBar", "bool", "android");
        if (rid != 0) {
            resourceId = context.getResources().getIdentifier("navigation_bar_height", "dimen", "android");
            return context.getResources().getDimensionPixelSize(resourceId);
        }
        return 0;
    }

解决后的视图

1.虚拟键盘弹出时WebView输入框被挡住 2.底部虚拟菜单栏弹出会挡住界面_第4张图片
1.虚拟键盘弹出时WebView输入框被挡住 2.底部虚拟菜单栏弹出会挡住界面_第5张图片
1.虚拟键盘弹出时WebView输入框被挡住 2.底部虚拟菜单栏弹出会挡住界面_第6张图片
1.虚拟键盘弹出时WebView输入框被挡住 2.底部虚拟菜单栏弹出会挡住界面_第7张图片

你可能感兴趣的:(Android)