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为状态栏高度