How to toggle the Statusbar?

余生颓废 提交于 2019-12-12 09:08:47

问题


I'm looking for a way to show and hide the statusbar with an onClickListener, but only showing it works.

WindowManager.LayoutParams lp = getWindow().getAttributes();
if (isStatusbarVisible)
    lp.flags = LayoutParams.FLAG_FULLSCREEN;
else
    lp.flags = LayoutParams.FLAG_FORCE_NOT_FULLSCREEN;
getWindow().setAttributes(lp);
isStatusbarVisible = !isStatusbarVisible;

Hiding the statusbar using FLAG_FULLSCREEN seems to work only if the flag is set before calling setContentView().

Is there another way to hide the statusbar?


回答1:


The answer is pretty simple, clearing the FLAG_FULLSCREEN flag is all thats necessary:

if (isStatusBarVisible)
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
else
    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);



回答2:


I've been looking for this solution, and finally came up with this implementation to toggle full screen on and off:

private void toggleFullscreen() {
    WindowManager.LayoutParams attrs = getWindow().getAttributes();
    attrs.flags ^= WindowManager.LayoutParams.FLAG_FULLSCREEN;
    getWindow().setAttributes(attrs);
}

It uses bitwise XOR logic to toggle FLAG_FULLSCREEN.




回答3:


As we have all seen, the Android API is constantly changing. Here's a more current answer to the question.

According to this doc: https://developer.android.com/training/system-ui/status.html

Hide the Status Bar on Android 4.1 and Higher

You can hide the status bar on Android 4.1 (API level 16) and higher by using setSystemUiVisibility(). setSystemUiVisibility() sets UI flags at the individual view level; these settings are aggregated to the window level. Using setSystemUiVisibility() to set UI flags gives you more granular control over the system bars than using WindowManager flags. This snippet hides the status bar:

View decorView = getWindow().getDecorView();
// Hide the status bar.
int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
// Remember that you should never show the action bar if the
// status bar is hidden, so hide that too if necessary.
ActionBar actionBar = getActionBar();
actionBar.hide();

Borrowing the groovy XOR from the other answers:

public void toggleFullscreen() {
    View decorView = getWindow().getDecorView();

    int uiOptions = decorView.getSystemUiVisibility();

    uiOptions ^= View.SYSTEM_UI_FLAG_FULLSCREEN;

    decorView.setSystemUiVisibility(uiOptions);
}

This does work (I am using it) and appears to function the same as the getWindow().setAttributes() solution, but it is the recommended way in the Developer docs.




回答4:


There's a better (does not require that boolean variable) toggle full screen method implementation:

private void toggleFullscreen() {
    WindowManager.LayoutParams attrs = getWindow().getAttributes();
    attrs.flags ^= WindowManager.LayoutParams.FLAG_FULLSCREEN;
    getWindow().setAttributes(attrs);
}

It uses bitwise XOR logic to toggle FLAG_FULLSCREEN.



来源:https://stackoverflow.com/questions/3856423/how-to-toggle-the-statusbar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!