美文网首页
输入框根据键盘自动调整位置

输入框根据键盘自动调整位置

作者: 搬砖的码农丶 | 来源:发表于2019-07-11 10:46 被阅读0次
        View decorView = getWindow().getDecorView();
        View contentView = findViewById(Window.ID_ANDROID_CONTENT);
        decorView.getViewTreeObserver()
                 .addOnGlobalLayoutListener(getGlobalLayoutListener(decorView, contentView));
 private ViewTreeObserver.OnGlobalLayoutListener getGlobalLayoutListener(final View decorView,
        final View contentView) {
        return new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                Rect r = new Rect();
                decorView.getWindowVisibleDisplayFrame(r);

                int height = decorView.getRootView().getHeight();
                int diff = height - r.bottom;

                if (diff != 0) {
                    if (contentView.getPaddingBottom() != diff) {
                        contentView.setPadding(0, getStatusBarHeight(), 0,
                            diff);
                    }
                } else {
                    if (contentView.getPaddingBottom() != 0) {
                        contentView.setPadding(0, getStatusBarHeight(), 0, 0);
                    }
                }
            }
        };
    }

其中getStatusBarHeight为状态栏高度

相关文章

网友评论

      本文标题:输入框根据键盘自动调整位置

      本文链接:https://www.haomeiwen.com/subject/fwlrkctx.html