天天看點

解決安卓全屏“FLAG_FULLSCREEN”狀态下“adjustResize”失效,全屏狀态下WebView的輸入框被軟鍵盤擋住的問題

沿着這個問題的線索,可以追溯到: http://code.google.com/p/android/issues/detail?id=5497    ,安卓官方問題回饋帖,這個問題的代号為“ 5497 ” ,就這個問題帖的回複來看,該問題困惑了許多人數年之久,問題釋出日期“ Dec 16, 2009 ”,現在我在安卓 4.4.2 環境運作,這個問題依舊存在。在此推薦這其中的一個解決方法,來自:stackoverflow.com,實測有效。 [java]  view plain copy print ?

  1. public class AndroidBug5497Workaround {  
  2.     // For more information, see https://code.google.com/p/android/issues/detail?id=5497  
  3.     // To use this class, simply invoke assistActivity() on an Activity that already has its content view set.  
  4.     public static void assistActivity (Activity activity) {  
  5.         new AndroidBug5497Workaround(activity);  
  6.     }  
  7.     private View mChildOfContent;  
  8.     private int usableHeightPrevious;  
  9.     private FrameLayout.LayoutParams frameLayoutParams;  
  10.     private AndroidBug5497Workaround(Activity activity) {  
  11.         FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content);  
  12.         mChildOfContent = content.getChildAt(0);  
  13.         mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {  
  14.             public void onGlobalLayout() {  
  15.                 possiblyResizeChildOfContent();  
  16.             }  
  17.         });  
  18.         frameLayoutParams = (FrameLayout.LayoutParams) mChildOfContent.getLayoutParams();  
  19.     }  
  20.     private void possiblyResizeChildOfContent() {  
  21.         int usableHeightNow = computeUsableHeight();  
  22.         if (usableHeightNow != usableHeightPrevious) {  
  23.             int usableHeightSansKeyboard = mChildOfContent.getRootView().getHeight();  
  24.             int heightDifference = usableHeightSansKeyboard - usableHeightNow;  
  25.             if (heightDifference > (usableHeightSansKeyboard/4)) {  
  26.                 // keyboard probably just became visible  
  27.                 frameLayoutParams.height = usableHeightSansKeyboard - heightDifference;  
  28.             } else {  
  29.                 // keyboard probably just became hidden  
  30.                 frameLayoutParams.height = usableHeightSansKeyboard;  
  31.             }  
  32.             mChildOfContent.requestLayout();  
  33.             usableHeightPrevious = usableHeightNow;  
  34.         }  
  35.     }  
  36.     private int computeUsableHeight() {  
  37.         Rect r = new Rect();  
  38.         mChildOfContent.getWindowVisibleDisplayFrame(r);  
  39.         return (r.bottom - r.top);  
  40.     }  
  41. }  

在Activity/Fragment的onCreate()/onCreateView()裡調用AndroidBug5497Workaround.assistActivity(Activity);代碼搬運,希望能夠幫助到各位。感謝答案提供者,祝生活愉快。