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

沿着這個問題的線索,可以追溯到:http://code.google.com/p/android/issues/detail?id=5497   ,安卓官方問題回饋帖,這個問題的代號爲“5497” ,就這個問題帖的回覆來看,該問題困惑了許多人數年之久,問題發佈日期“Dec 16, 2009”,現在我在安卓4.4.2環境運行,這個問題依舊存在。在此推薦這其中的一個解決方法,來自:stackoverflow.com,實測有效。
  1. public class AndroidBug5497Workaround {  
  2.   
  3.     // For more information, see https://code.google.com/p/android/issues/detail?id=5497  
  4.     // To use this class, simply invoke assistActivity() on an Activity that already has its content view set.  
  5.   
  6.     public static void assistActivity (Activity activity) {  
  7.         new AndroidBug5497Workaround(activity);  
  8.     }  
  9.   
  10.     private View mChildOfContent;  
  11.     private int usableHeightPrevious;  
  12.     private FrameLayout.LayoutParams frameLayoutParams;  
  13.   
  14.     private AndroidBug5497Workaround(Activity activity) {  
  15.         FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content);  
  16.         mChildOfContent = content.getChildAt(0);  
  17.         mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {  
  18.             public void onGlobalLayout() {  
  19.                 possiblyResizeChildOfContent();  
  20.             }  
  21.         });  
  22.         frameLayoutParams = (FrameLayout.LayoutParams) mChildOfContent.getLayoutParams();  
  23.     }  
  24.   
  25.     private void possiblyResizeChildOfContent() {  
  26.         int usableHeightNow = computeUsableHeight();  
  27.         if (usableHeightNow != usableHeightPrevious) {  
  28.             int usableHeightSansKeyboard = mChildOfContent.getRootView().getHeight();  
  29.             int heightDifference = usableHeightSansKeyboard - usableHeightNow;  
  30.             if (heightDifference > (usableHeightSansKeyboard/4)) {  
  31.                 // keyboard probably just became visible  
  32.                 frameLayoutParams.height = usableHeightSansKeyboard - heightDifference;  
  33.             } else {  
  34.                 // keyboard probably just became hidden  
  35.                 frameLayoutParams.height = usableHeightSansKeyboard;  
  36.             }  
  37.             mChildOfContent.requestLayout();  
  38.             usableHeightPrevious = usableHeightNow;  
  39.         }  
  40.     }  
  41.   
  42.     private int computeUsableHeight() {  
  43.         Rect r = new Rect();  
  44.         mChildOfContent.getWindowVisibleDisplayFrame(r);  
  45.         return (r.bottom - r.top);  
  46.     }  
  47.   
  48. }  

在Activity/Fragment的onCreate()/onCreateView()裏調用AndroidBug5497Workaround.assistActivity(Activity);代碼搬運,希望能夠幫助到各位。感謝答案提供者,祝生活愉快。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章