publicclass WindowSoftModeAdjustResizeExecutor {// For more information, see https://code.google.com/p/android/issues/detail?id=5497// To use this class, simply invoke assistActivity() on an Activity that already has its content view set.// CREDIT TO Joseph Johnson (http://stackoverflow.com/users/341631/joseph-johnson) for publishing the original Android solution on stackoverflow.compublicstaticvoidassistActivity(Activity activity) {new WindowSoftModeAdjustResizeExecutor(activity);}private View mChildOfContent;privateint usableHeightPrevious;private FrameLayout.LayoutParams frameLayoutParams;privateWindowSoftModeAdjustResizeExecutor(Activity activity) {FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content);mChildOfContent = content.getChildAt(0);mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {publicvoidonGlobalLayout() {possiblyResizeChildOfContent();}});frameLayoutParams = (FrameLayout.LayoutParams) mChildOfContent.getLayoutParams();}privatevoidpossiblyResizeChildOfContent() {int usableHeightNow = computeUsableHeight();if (usableHeightNow != usableHeightPrevious) {int usableHeightSansKeyboard = mChildOfContent.getRootView().getHeight();int heightDifference = usableHeightSansKeyboard - usableHeightNow;frameLayoutParams.height = usableHeightSansKeyboard - heightDifference;mChildOfContent.requestLayout();usableHeightPrevious = usableHeightNow;}}privateintcomputeUsableHeight() {Rect r = new Rect();mChildOfContent.getWindowVisibleDisplayFrame(r);if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {return (r.bottom - r.top);}return r.bottom;}
}