全屏显示的包含webview的页面中弹出的软键盘覆盖输入框的问题
2019獨角獸企業重金招聘Python工程師標準>>>
備注 1.使用adjustResize屬性時,如果界面中沒有滾動條,需要添加一個滾動條scrollview包裹所有內容,保證resize后 能滾動顯示顯示不下的內容 2.全屏fullscreen模式時 adjustResize屬性失效,屬于是一個bug,只能使用adjustPan 來設置焦點 3.全屏fullscreen模式時 webview adjustPan 偶爾也會失效
首先,在全屏的狀態下,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"也不一定會起作用。
http://stackoverflow.com/questions/7417123/android-how-to-adjust-layout-in-full-screen-mode-when-softkeyboard-is-visible/19494006#19494006在此找到了解決方法。
<!-- 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 visibleframeLayoutParams.height = usableHeightSansKeyboard - heightDifference;} else {// keyboard probably just became hiddenframeLayoutParams.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可以正常使用,應該是個別系統問題
轉載于:https://my.oschina.net/u/1409622/blog/310631
總結
以上是生活随笔為你收集整理的全屏显示的包含webview的页面中弹出的软键盘覆盖输入框的问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: What is Proguard?
- 下一篇: 【HDOJ1043】八数码的八境界