简体   繁体   中英

Force android libgdx into immersive mode

Following the tutorials on this page(written in 2009) don't work any more. http://www.androidsnippets.com/how-to-make-an-activity-fullscreen

public class FullScreen extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.main);
    }
}

Similarily, trying to change the theme in android.xml has no effect: android:theme="@android:style/Theme.NoTitleBar.Fullscreen"

Here is a screenshot: 在此输入图像描述

After scouring the web for a few hours, i cant find any articles that can help me. So how, in version 1.5.4 of libgdx, can i get an immersive full screen ?

----EDIT---- Here is the now fully working code:

public class AndroidLauncher extends AndroidApplication {
    @Override
    protected void onCreate (Bundle savedInstanceState) {
        hideSystemUi();

        super.onCreate(savedInstanceState);
        AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
        config.hideStatusBar=true;
        config.useImmersiveMode=true;
        initialize(new game(), config);
    }


    private void hideSystemUi() {
        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
            | View.SYSTEM_UI_FLAG_FULLSCREEN
            | View.SYSTEM_UI_FLAG_IMMERSIVE);
    }
}

Try this in the android activity:

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    if (hasFocus) {
        getWindow().getDecorView().setSystemUiVisibility(
            View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
            View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
            View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
            View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
    }
}

Note this will only work on API level 19 or above.

Sadly I can't comment due to my current reputation, however I have found that adding:

config.hideStatusBar = true;
config.useImmersiveMode = true;

in the AndroidLauncher class is enough to show the app in immersive however the app will start in standard mode and then update to immersive mode, more info on the Android immersive mode:

When you use the SYSTEM_UI_FLAG_IMMERSIVE_STICKY flag, an inward swipe in the system bars areas causes the bars to temporarily appear in a semi-transparent state, but no flags are cleared, and your system UI visibility change listeners are not triggered. The bars automatically hide again after a short delay, or if the user interacts with the middle of the screen.

Android sdk page
So my conclusion would be that config.useImmersiveMode use the sticky flag.

On a side note, @TargetApi(Build.VERSION_CODES.HONEYCOMB) has to be added to the OnWindowFocusChanged method when targeting an api lvl less than 11

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM