Android软键盘遮挡控件

Android大约 859 字

解决方法

public void addLayoutListener(final View main, final View scroll) {
    main.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            Rect rect = new Rect();
            main.getWindowVisibleDisplayFrame(rect);
            int mainInvisibleHeight = main.getRootView().getHeight() - rect.bottom;
            int screenHeight = main.getRootView().getHeight();//屏幕高度
            if (mainInvisibleHeight > screenHeight / 4) {
                int[] location = new int[2];
                scroll.getLocationInWindow(location);
                int srollHeight = (location[1] + scroll.getHeight()) - rect.bottom;
                main.scrollTo(0, srollHeight);
            } else {
                main.scrollTo(0, 0);
            }
        }
    });
}
阅读 274 · 发布于 2019-04-10

————        END        ————

扫描下方二维码关注公众号和小程序↓↓↓

昵称:
随便看看换一批