因为在全屏模式下 是不好使的
需要在activity setContentView(R.layout.XXXX)之后调用;
AndroidBug5497Workaround.assistActivity(this);
importandroid.app.Activity;
importandroid.graphics.Rect;
importandroid.view.View;
importandroid.view.ViewTreeObserver;
importandroid.widget.FrameLayout;
public classAndroidBug5497Workaround {
// 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.
public static voidassistActivity(Activity activity) {
newAndroidBug5497Workaround(activity);
}
privateViewmChildOfContent;
private intusableHeightPrevious;
privateFrameLayout.LayoutParamsframeLayoutParams;
privateAndroidBug5497Workaround(finalActivity activity) {
FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content);
mChildOfContent= content.getChildAt(0);
mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(newViewTreeObserver.OnGlobalLayoutListener() {
public voidonGlobalLayout() {
possiblyResizeChildOfContent(activity);
}
});
frameLayoutParams= (FrameLayout.LayoutParams)mChildOfContent.getLayoutParams();
}
private voidpossiblyResizeChildOfContent(Activity activity) {
intusableHeightNow = computeUsableHeight(activity);
if(usableHeightNow !=usableHeightPrevious) {
intusableHeightSansKeyboard =mChildOfContent.getRootView().getHeight();
intheightDifference = 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(Activity activity) {
Rect r =newRect();
mChildOfContent.getWindowVisibleDisplayFrame(r);
if(r.top==0){
r.top=getStatusBarHeight(activity);//状态栏目的高度
}
return(r.bottom- r.top);
}
public intgetStatusBarHeight(Activity activity) {
intresult =0;
intresourceId = activity.getResources().getIdentifier("status_bar_height","dimen","android");
if(resourceId >0) {
result = activity.getResources().getDimensionPixelSize(resourceId);
}
returnresult;
}
}
网友评论