Three-step immersive status bar (i.e. status bar and app color)

Source: Internet
Author: User

This article implements the immersive status bar. What is the status bar before you define the concept? Direct:

What is an immersive status bar? The example diagram is as follows:

Non-immersive status bar immersive status bar

In fact, the real thing is to make the color of the mobile phone status bar change, so that it becomes the color of their own. If you set it to the same color as the app's main tone, it will look a lot better. As for the name, the internet is so called, you don't have to delve into it. Here's how it's implemented, in fact it's simple, just three steps:

premise: The built-in Android project must be API 19 above, that is, Android4.4 version above.

Step 1:

Add two properties to the outermost layout of the layouts file.

Android:cliptopadding= "true"
Android:fitssystemwindows= "true"

Example:

Step 2:

Add two lines of code to the activity's OnCreate () method:

GetWindow (). Addflags (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); Transparent status bar
GetWindow (). Addflags (WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); Transparent navigation bar

Step 3 :

Add a line of code to the activity's OnCreate () method:

Requestwindowfeature (Window.feature_no_title); Set no title bar

Note: This code must be added to the Setcontentview (r.layout.activity_main);

Example:

Through the above three simple operation, so that you can achieve the status bar color!


remark:

First, in fact, only need to join

GetWindow (). Addflags (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); Transparent status bar
GetWindow (). Addflags (WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); Transparent navigation bar

These two lines of code can change the status bar color.

However, there is a situation.

condition 1 (if the layout file does not add android:cliptopadding= "true" android:fitssystemwindows= "true" for both properties)

As you can see, the title bar moves upwards, occupying the space of the status bar. This means that the status bar is part of the app's title bar.


Condition 2 (if the OnCreate () method does not add   requestwindowfeature (window.feature_no_title); This code )

You can see that the layout of the system comes with the title bar, generally this is not required.

ii. in step 3, Requestwindowfeature (Window.feature_no_title); The setup of this code can not be set in the OnCreate () method, the theme style set to the configuration file is also possible, but to match your activity, the example is as follows:

The color of the status bar is the background color of the outermost layout in the layout file. However, if the outermost layout setting is match_parent wide, and its sub-layouts do not have any background colors set,

This will make the entire screen (including the status bar) a color,

Such as:

Of course, sometimes we need to set the content outside the title area to a different color than the app's main color. At this point, just set the background color of the sub-layout that needs to change the background color.

The results are as follows:

Usually, this is the effect I want. Everyone needs different, we play by ourselves.


Three-step immersive status bar (i.e. status bar and app color)

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.