public classAndroidBug5497Workaround {//For more information, seehttps://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.

public static voidassistActivity (Activity activity) {newAndroidBug5497Workaround(activity);

}privateView mChildOfContent;private intusableHeightPrevious;privateFrameLayout.LayoutParams frameLayoutParams;privateAndroidBug5497Workaround(Activity activity) {

FrameLayout content=(FrameLayout) activity.findViewById(android.R.id.content);

mChildOfContent= content.getChildAt(0);

mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(newViewTreeObserver.OnGlobalLayoutListener() {public voidonGlobalLayout() {

possiblyResizeChildOfContent();

}

});

frameLayoutParams=(FrameLayout.LayoutParams) mChildOfContent.getLayoutParams();

}private voidpossiblyResizeChildOfContent() {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

frameLayoutParams.height =usableHeightSansKeyboard;

}

mChildOfContent.requestLayout();

usableHeightPrevious=usableHeightNow;

}

}private intcomputeUsableHeight() {

Rect r= newRect();

mChildOfContent.getWindowVisibleDisplayFrame(r);return (r.bottom -r.top);

}

}

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐