Android Official Development Document Training series: Hidden status bar for managing system UI

Source: Internet
Author: User

Original address: http://android.xsoftlab.net/training/system-ui/status.html

This session will show you how to hide different versions of the status bar. Hiding the status bar can make the content presentation area larger, so you can provide a more immersive user experience.

Apps that contain status bars:

Hide the status bar app, note that the actionbar here is also hidden. Never show Actionbar when there is no status bar:

Hide status bar in versions below Android 4.0

Developers can hide the status bar before Android 4.0 by setting the WindowManager flag. In addition to setting the activity theme in the manifest file, the same effect can be achieved. If it is necessary to keep the status bar hidden, it is our preferred way to set the theme in the manifest file:

<application    ...    android:theme="@android:style/Theme.Holo.NoActionBar.Fullscreen" >    ...</application>

The advantages of using the activity theme are:

    • Easier to maintain versus dynamic settings and reduces the risk of errors.
    • This makes the UI transitions smoother because J has already got the UI rendering information it needs (status bar shadowing, etc.) before instantiating the activity.

Developers can achieve the same goal by setting the WindowManager flag. This approach is useful when users interact with the app.

 Public  class mainactivity extends Activity {    @Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);//If The Android version is lower than Jellybean        //the status bar.        if(Build.VERSION.SDK_INT < -) {GetWindow (). SetFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN, Windowmanager.layoutpar Ams.        Flag_fullscreen);    } setcontentview (R.layout.activity_main); }    ...}

When you set the WindowManager flag (either through the activity's theme or the dynamic setting), the flag remains until the flag is removed.

Hide status bar in Android version 4.1 or above

You can hide the status bar of Android 4.1 or above by using the Setsystemuivisibility () method. Setsystemuivisibility () Sets the UI flags for a single view level, which is integrated into the window hierarchy. Setsystemuivisibility () has finer control granularity than WindowManager. The following code 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();

Here are a few things to keep in mind:

    • Once the UI flags are cleared (for example, by navigating away from the activity), you'll need to reset the flags if you want to hide the status bar again.
    • There are some differences in setting the UI flags in different places. If the system bar is hidden in the activity's OnCreate () method and the user returns to the desktop, the system bar appears again. When the user re-opens the activity, onCreate () is not called again, so the system bar remains visible. If you want the system bar to remain in its original state, you need to set the appropriate flag in Onresume () or onwindowfocuschanged ().
    • Setsystemuivisibility () is only valid if the view is visible.
    • Navigating away from view causes the flag set by Setsystemuivisibility () to be cleared.
Make the content appear behind the status bar

In Android 4.1 or later, you can display the app's content area behind the status bar, so the size of the content area does not change as the status bar is hidden. This is achieved by using the SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN flag. You may also need to use the SYSTEM_UI_FLAG_LAYOUT_STABLE flag to help the app maintain a stable layout state.

When you use this method, it is your responsibility to ensure that the boundary portions of the app UI are not obscured by the system bar. In many cases, you can handle this situation by adding the Android:fitssystemwindows=true property to the layout file. It adjusts the inner margin of the parent ViewGroup to allow space for the system window, which is enough to handle most applications.

With the transformation of Actionbar the State bar is synchronized

In Android 4.1 and above, to avoid resizing the layout, when Actionbar is displayed or hidden, you can turn on Actionbar's overlay Mode. When under overlay mode, the activity layout uses all available space, as if the Actionbar does not exist, and the system draws the Actionbar on the top layer of the layout. This blurs the top part of the layout, but when Actionbar is displayed or hidden, the system does not rewrite the size of the layout, which makes the transition process seamless.

To enable Actionbar to open overlay Mode, you need to create a custom theme and inherit the existing theme that carries Actionbar, and you need to set the Android:windowactionbaroverlay property to True.

Android Official Development Document Training series: Hidden status bar for managing system UI

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.