参考资料: https://blog.csdn.net/qq_26665903/article/details/52026732

找了很久才找到一个合适的解决方案, 于是怒赞了一波原作者, 并且为本博客又摘录(抄?)了一篇优秀的博客回来. 啊哈哈..

因为我的需求是在 Fragment 中使用这种效果, 所以代码与原作者代码有些区别. mActivity 来自当前 Fragment.getActivity(), 新手请知悉.

下面贴下代码 :

View decorView = mActivity.getWindow().getDecorView();
View contentView = mActivity.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);
                    }
                }
            }
        };
    }