private int usableHeightPrevious; private RelativeLayout relativeLayout; private View mChildOfContent;
relativeLayout = (RelativeLayout) findViewById(R.id.RelativeLayout); mChildOfContent = relativeLayout.getChildAt(0); usableHeightPrevious = computeUsableHeight(); mChildOfContent.getViewTreeObserver() .addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { public void onGlobalLayout() { possiblyResizeChildOfContent(); }});
private void possiblyResizeChildOfContent() { int usableHeightNow = computeUsableHeight(); if (usableHeightNow != usableHeightPrevious) { int usableHeightSansKeyboard = mChildOfContent.getRootView().getHeight(); int heightDifference = usableHeightSansKeyboard - usableHeightNow; if (heightDifference > (usableHeightSansKeyboard / 4)) { L.e("键盘弹出"); } else { L.e("键盘收起"); hideBottomUIMenu(); } mChildOfContent.requestLayout(); usableHeightPrevious = usableHeightNow; } } private int computeUsableHeight() { Rect r = new Rect(); mChildOfContent.getWindowVisibleDisplayFrame(r); return (r.bottom - r.top);}