備註 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