记录资源 解决软键盘遮挡问题

来源:互联网 发布:java组件设计 编辑:程序博客网 时间:2024/05/23 17:30
解决android1.0x到7.0键盘的BUG 添加纪录 
public class AndroidBug5497Workaround {    public static void assistActivity (Activity activity) {        new AndroidBug5497Workaround(activity);    }    private View mChildOfContent;    private int usableHeightPrevious;    private FrameLayout.LayoutParams frameLayoutParams;    private AndroidBug5497Workaround(final Activity activity) {        FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content);        content.setBackgroundResource(R.color.white_ff);        mChildOfContent = content.getChildAt(0);        mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {            public void onGlobalLayout() {                possiblyResizeChildOfContent(activity);            }        });        frameLayoutParams = (FrameLayout.LayoutParams) mChildOfContent.getLayoutParams();    }    private void possiblyResizeChildOfContent(Activity activity) {        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+ScreenUtils.getStatusHeight(activity);            } else {                // keyboard probably just became hidden                frameLayoutParams.height = usableHeightSansKeyboard;            }            mChildOfContent.requestLayout();            usableHeightPrevious = usableHeightNow;        }    }    private int computeUsableHeight() {        Rect r = new Rect();        mChildOfContent.getWindowVisibleDisplayFrame(r);        return (r.bottom - r.top);    }}
原创粉丝点击