Android - Webview in fullscreen mode how to hide n

2019-05-12 01:44发布

问题:

I have developed a small Android app using a webview. All Android UI elements such as notificationbar, statusbar, actionbar are hidden using:

  private void hideSystemUI() {
// Set the IMMERSIVE flag.
// Set the content to appear under the system bars so that the content
// doesn't resize when the system bars hide and show.
getWindow().getDecorView().setSystemUiVisibility(
    View.SYSTEM_UI_FLAG_LAYOUT_STABLE
        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar
        | View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
        | View.SYSTEM_UI_FLAG_IMMERSIVE);

}

If I open an HTML formular and tap into one input field, the softkeyboard shows up. But then also the Android notificationbar appears, what I don't want. (see images: http://imgur.com/a/cKWg8#0) If I close the softkeyboard by the upper left key on the softkeyboard, the notificationbar still remains open and occupies a part of my title bar on my HTML page. How can I hide the notificationbar if softkeyboard is opened?

Thanks!

回答1:

This works for me. Call this in onCreate:

private void setupFullscreenMode() {
    View decorView = setFullscreen();
    decorView
            .setOnSystemUiVisibilityChangeListener(new OnSystemUiVisibilityChangeListener() {
                @Override
                public void onSystemUiVisibilityChange(int visibility) {
                    setFullscreen();
                }
            });
}

private View setFullscreen() {
    View decorView = getWindow().getDecorView();
    decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
            | View.SYSTEM_UI_FLAG_FULLSCREEN
            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
    return decorView;
}

Also override onWindowsFocusChanged:

public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    if (hasFocus) {
        setFullscreen();
    }
}


回答2:

I had the same problem, solved it by doing this:

    mWebView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            hideSystemUI(); // setup fullscreen
        }
    });