处理当调用软键盘没有把底部布局顶上去时!

由于弹出软键盘挡住了布局,很不舒服,对用户体验也不好,所以需要解决!

1、在清单文件的application节点内添加
android:windowSoftInputMode=”adjustResize|adjustUnspecified|stateHidden”

2、在需要的界面上添加

 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.getContext().getResources().getDisplayMetrics().heightPixels;
                int diff = height - r.bottom;
                if (diff != 0) {
                    if (contentView.getPaddingBottom() != diff) {
                        contentView.setPadding(0, 0, 0, diff);
                    }
                } else {
                    if (contentView.getPaddingBottom() != 0) {
                        contentView.setPadding(0, 0, 0, 0);
                    }
                }
            }
        };
    }

猜你喜欢

转载自blog.csdn.net/qq_33373648/article/details/79664201
今日推荐