Android 解决全面屏 软键盘弹出会有高度约50dp的白条。

效果对比图片先走

解决前效果图
Android 解决全面屏 软键盘弹出会有高度约50dp的白条。_第1张图片
T_X0~JU{WQ2FC1W$M})J%1S.jpg
解决完成后效果图
Android 解决全面屏 软键盘弹出会有高度约50dp的白条。_第2张图片
0EBFD68FC050B16866B81321E0FD9C62.jpg
/**
 * @author colin
 * @Date 2020/7/20
 * @ClassTag :
 */

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(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;

            }

            mChildOfContent.requestLayout();

            usableHeightPrevious = usableHeightNow;

        }

    }

    private int computeUsableHeight() {

        Rect r = new Rect();

        mChildOfContent.getWindowVisibleDisplayFrame(r);

        return (r.bottom - r.top);// 全屏模式下:return r.bottom

    }

}

使用方式

Android 解决全面屏 软键盘弹出会有高度约50dp的白条。_第3张图片
image.png
//在Activity中初始化
  AndroidBug5497Workaround.assistActivity(this);
     
        

一定要在 清单文件对应的Activity中设置 adjustPan属性

你可能感兴趣的:(Android 解决全面屏 软键盘弹出会有高度约50dp的白条。)