沉浸式状态栏与界面adjustResize之间的bug解决方案

来源:互联网 发布:windows live软件包 编辑:程序博客网 时间:2024/06/03 19:20
public class AndroidBug5497Workaround {    public static void assistActivity (Activity activity) {        new AndroidBug5497Workaround(activity);    }    private View mChildOfContent;    private int usableHeightPrevious;    private FrameLayout.LayoutParams frameLayoutParams;    private AndroidBug5497Workaround(Activity activity) {        FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content);        mChildOfContent = content.getChildAt(0);        mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {            public void onGlobalLayout() {                possiblyResizeChildOfContent();            }        });        frameLayoutParams = (FrameLayout.LayoutParams) mChildOfContent.getLayoutParams();    }    private void possiblyResizeChildOfContent() {        int usableHeightNow = computeUsableHeight();        if (usableHeightNow != usableHeightPrevious) {            int usableHeightSansKeyboard = mChildOfContent.getRootView().getHeight();            int heightDifference = usableHeightSansKeyboard - usableHeightNow;            if (heightDifference > (usableHeightSansKeyboard/4)) {                // keyboard probably just became visible                frameLayoutParams.height = usableHeightSansKeyboard - heightDifference;            } else {                // keyboard probably just became hidden                Rect r = new Rect();                mChildOfContent.getWindowVisibleDisplayFrame(r);                frameLayoutParams.height = usableHeightSansKeyboard- r.top;            }            mChildOfContent.requestLayout();            usableHeightPrevious = usableHeightNow;        }    }    private int computeUsableHeight() {        Rect r = new Rect();        mChildOfContent.getWindowVisibleDisplayFrame(r);       // - r.top        return (r.bottom - r.top);    }}
在Oncreate里面调用
AndroidBug5497Workaround.assistActivity(this);
问答题就解决了

                                             
0 0