importandroid.app.Activity;
importandroid.content.Context;
importandroid.graphics.Rect;
importandroid.util.DisplayMetrics;
importandroid.view.Display;
importandroid.view.View;
importandroid.view.ViewTreeObserver;
importandroid.view.WindowManager;
importandroid.widget.FrameLayout;
importjava.lang.reflect.Method;
public class AndroidBug5497Workaround {
Contextcontext;
// 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,Context context) {
newAndroidBug5497Workaround(activity,context);
}
privateViewmChildOfContent;
private intusableHeightPrevious;
privateFrameLayout.LayoutParamsframeLayoutParams;
privateAndroidBug5497Workaround(Activity activity,Context context) {
this.context= context;
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() {
intusableHeightNow = computeUsableHeight();
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-getBottomStatusHeight(context);
}
mChildOfContent.requestLayout();
usableHeightPrevious= usableHeightNow;
}
}
private intcomputeUsableHeight() {
Rect r =newRect();
mChildOfContent.getWindowVisibleDisplayFrame(r);
return(r.bottom);
}
public static intgetDpi(Context context){
intdpi =0;
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
DisplayMetrics displayMetrics =newDisplayMetrics();
@SuppressWarnings("rawtypes")
Class c;
try{
c = Class.forName("android.view.Display");
@SuppressWarnings("unchecked")
Method method = c.getMethod("getRealMetrics",DisplayMetrics.class);
method.invoke(display, displayMetrics);
dpi=displayMetrics.heightPixels;
}catch(Exception e){
e.printStackTrace();
}
returndpi;
}
/**
* 获取 虚拟按键的高度
*@paramcontext
*@return
*/
public static intgetBottomStatusHeight(Context context){
inttotalHeight =getDpi(context);
intcontentHeight =getScreenHeight(context);
returntotalHeight - contentHeight;
}
/**
* 获得屏幕高度
*
*@paramcontext
*@return
*/
public static intgetScreenHeight(Context context)
{
WindowManager wm = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics outMetrics =newDisplayMetrics();
wm.getDefaultDisplay().getMetrics(outMetrics);
returnoutMetrics.heightPixels;
}
}