来源:https://blog.youkuaiyun.com/passerby_b/article/details/82686662,https://www.jianshu.com/p/a95a1b84da11
最近开始写博客,突然记起之前解决的webview+editview+scrollview导致的问题,来源我大概找了找,如果拉了谁,勿怪勿怪,当然也加了点自己的,直接上代码:
public class AndroidBug5497Workaround { public static void assistActivity(Activity activity) { new AndroidBug5497Workaround(activity); } private View mChildOfContent; private int usableHeightPrevious; private FrameLayout.LayoutParams frameLayoutParams; private int barStatusHeight = 0;//状态栏高度 private AndroidBug5497Workaround(Activity activity) { FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content); mChildOfContent = content.getChildAt(0); mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { public void onGlobalLayout() { possiblyResizeChildOfContent(); } }); frameLayoutParams = (FrameLayout.LayoutParams) mChildOfContent.getLayoutParams(); barStatusHeight = getStatusBarHeight(activity); } private void possiblyResizeChildOfContent() { int usableHeightNow = computeUsableHeight(); if (usableHeightNow != usableHeightPrevious) { usableHeightPrevious = usableHeightNow;//防止重复变动 // int heightDecor = mChildOfContent.getHeight();//不含虚拟按键的高度,貌似不包含状态栏高度 int heightRoot = mChildOfContent.getRootView().getHeight();//包含虚拟按键的高度(如果有的话) int heightDifference = heightRoot - usableHeightNow; if (heightDifference > (heightRoot / 4)) {//高度变动超过decor的四分之一则认为是软键盘弹出事件,为什么不用屏幕高度呢?开始以为这样在分屏模式下也可以监听,但是实测不行。 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { setHeight(heightRoot - heightDifference + barStatusHeight);//这里为什么要添加状态栏高度? } else { setHeight(heightRoot - heightDifference);//这里不添加状态栏高度?不懂为什么,原版如此,就先这样吧。遇到再说~ } } else { setHeight(FrameLayout.LayoutParams.MATCH_PARENT);//还原默认高度,不能用计算的值,因为虚拟导航栏显示或者隐藏的时候也会改变高度 } } } private void setHeight(int height) { if (frameLayoutParams.height != height) {//不必要的更新就不要了 frameLayoutParams.height = height; mChildOfContent.requestLayout();//触发布局更新 } } /** * 获取状态栏高度 * * @param context context * @return 状态栏高度 */ private static int getStatusBarHeight(Activity context) { // 获得状态栏高度 return getBarHeight(context, "status_bar_height"); } /** * 获取Bar高度 * * @param context context * @param barName 名称 * @return Bar高度 */ private static int getBarHeight(Context context, String barName) { // 获得状态栏高度 int resourceId = context.getResources().getIdentifier(barName, "dimen", "android"); return context.getResources().getDimensionPixelSize(resourceId); } private int computeUsableHeight() { Rect r = new Rect(); mChildOfContent.getWindowVisibleDisplayFrame(r); return (r.bottom - r.top); } }
用法:
if (getActivity() != null)
AndroidBug5497Workaround.assistActivity(getActivity());
我方手机都测过了,木门提,希望能解决你的问题^_^。