备注 1.使用adjustResize属性时,若是界面中没有滚动条,须要添加一个滚动条scrollview包裹全部内容,保证resize后 能滚动显示显示不下的内容 2.全屏fullscreen模式时 adjustResize属性失效,属因而一个bug,只能使用adjustPan 来设置焦点 3.全屏fullscreen模式时 webview adjustPan 偶尔也会失效java
首先,在全屏的状态下,android:windowSoftInputMode="adjustResize"属性是不起做用的,官方文档中有记录: Window flag: hide all screen decorations (such as the status bar) while this window is displayed. This allows the window to use the entire display space for itself -- the status bar will be hidden when an app window with this flag set is on the top layer. A fullscreen window will ignore a value of SOFT_INPUT_ADJUST_RESIZE for the window's softInputMode field; the window will stay fullscreen and will not resize. 大意就是说在全屏状态下,会忽略SOFT_INPUT_ADJUST_RESIZE属性。而全屏状态下设置为android:windowSoftInputMode="adjustPan"也不必定会起做用。android
<!-- lang: java --> public class AndroidBug5497Workaround { // 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 void assistActivity (Activity activity) { new AndroidBug5497Workaround(activity); } private View mChildOfContent; private int usableHeightPrevious; private FrameLayout.LayoutParams frameLayoutParams; 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(); } private void possiblyResizeChildOfContent() { 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 int computeUsableHeight() { Rect r = new Rect(); mChildOfContent.getWindowVisibleDisplayFrame(r); return (r.bottom - r.top); } }
只要在setContentvView以后调用assistActivity 方法便可。 在具体使用时发现有时键盘弹出后点击空白处隐藏键盘,布局并无恢复全屏,发现是有时候OnGlobalLayoutListener监听并无被触发,后替换成OnPreDrawListener监听便可。 经多个模拟器与真机测试,发现OnGlobalLayoutListener能够正常使用,应该是个别系统问题app