1.复制这段代码在onCreate中,或者在自己需要的地方 View decorView = getWindow().getDecorView(); View contentView = findViewById(Window.ID_ANDROID_CONTENT); decorView.getViewTreeObserver().addOnGlobalLayoutListener(getGlobalLayoutListener(decorView, contentView));
2. /软键盘动态弹出 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); } } } }; }