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

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

沿著這個問題的線索,可以追溯到:http://code.google.com/p/android/issues/detail?id=5497   ,安卓官方問題回饋帖,這個問題的代號為“5497” ,就這個問題帖的回覆來看,該問題困惑了許多人數年之久,問題釋出日期“Dec 16, 2009”,現在我在安卓4.4.2環境執行,這個問題依舊存在。在此推薦這其中的一個解決方法,來自:stackoverflow.com,實測有效。
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);
    }

}

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