Android Development-Status bar coloring principle and API version number compatible processing

Source: Internet
Author: User
Tags getcolor

Introduced

First on the actual, there are three version number please note the API version number

api>=20 api=19 api<19

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvq2fyzdm2mtqwmtm3ng==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma== /dissolve/70/gravity/southeast "alt=" <19 "title=" ">

The above effect is what we call the coloring of the status bar, not the immersive status bar. That's something else.
To achieve the above effect is not difficult, read this article to understand the principle, you will think that want the above effect is really easy.
The purpose of this article is to summarize my understanding and tell the reader how to make the status bar coloring step-by-step.

First step of principle-set transparent status bar

Control of the status bar all bases are derived from

    static public void setTranslucentWindows(Activity activity) {        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {            //透明状态栏            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);        }    }

Note that the flag identifies the API version number 19, which means that only the transparent status bar can be implemented above api>=19, which is why the API is differentiated above.

Note: The following style style is the same as the code, but there are compatibility issues. It is recommended to use code settings directly
<item name="android:windowTranslucentStatus">true</item>

The second step-padding out the reserved space

After you set the transparency status bar, the content will top to the top of the screen and overlap the contents of the status bar.

You need to set the padding out to the same height as the status bar. Prevent overlap
There are two ways to resolve this:

    1. Set in XML: Api+14 to find the android:fitsSystemWindows="true" outermost view setting for a view setup system layout paddingtop

    2. Code control: Determine the status bar height and then set the value of Paddingtop to the required view

It is simpler to set properties directly in XML. But there are limitations, only valid for the outermost view. Assume that the fragment view is set fitssystemwindows and then added to the screen is not valid.


Code control, can solve the problem of fragment. But determine the status bar height is a bit troublesome, in general, the status bar height is 24DP, insurance or need to read the system's configuration height, there are settings padding sometimes will fail, it is necessary handler.post(new Runnable()) to ensure the operation of the Code

Read the system configuration status bar height code:
    /** * 19API above reading to the status bar height is meaningful * * @param context * @return  * *    Static  Public int Getstatusbarheight(Context context) {if(Build.VERSION.SDK_INT >= build.version_codes. KITKAT) {intResourceId = Context.getresources (). Getidentifier ("Status_bar_height","Dimen","Android");returnResourceId >0? Context.getresources (). Getdimensionpixelsize (resourceId):0; }Else{return 0; }    }
Step three-coloring

The results of the first two steps are described here.

    • API<19: Do not run
    • API=19: Black and gray progressive transparent status bar
    • API>=20: The system will actively generate a translucent status bar on its own

So we need to set the color of the reservation in the second step, the same two ways:

    1. Join view: The parent container for the entire view framelayout (id=android. R.id.content) Add a status bar with a color view. Overwrite to the status bar.
    2. Self-rendering: controls that have paddingtop set. Sets the background color. When the control is padding processed, the color is also rendered to the status bar at the same time.

Add the code that overrides the status bar view:
    /** * Set the status bar color for the app main color * Mate {@link #setTranslucentWindows (Activity)} method using * The Main method is to add a view and set the background color to add to the system Contentview Medium * * @param Activity * *    Static  Public void Addstatusbarbackground(Activity activity) {intHeight Height = getstatusbarheight (activity);if(Height <=0) {return; } framelayout layout = (framelayout) Activity.findviewbyid (Android.        R.id.content); Framelayout statuslayout =NewFramelayout (activity); Statuslayout.setlayoutparams (NewLinearlayout.layoutparams (ViewGroup.LayoutParams.MATCH_PARENT, height)); Typedvalue Typedvalue =NewTypedvalue (); TypedArray a = Activity.obtainstyledattributes (Typedvalue.data,New int[]{r.attr.colorprimary}];intcolor = A.getcolor (0,0);        A.recycle ();        Statuslayout.setbackgroundcolor (color);    Layout.addview (statuslayout); }
Special api21+.
当API21+:可以调用系统API直接对状态栏着色if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP){    this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);    this.getWindow().setStatusBarColor(ContextCompat.getColor(this,R.color.red_normal));}
Summarize

Implementing the status bar coloring is not complicated, it only takes three steps. The actual use is actually a few lines of code or several method calls. However, in the actual use of the need to consider the actual needs, compatibility issues. With the use of each step we can achieve the desired effect.

As the above implementation is: Activity status bar Transparent, navigation search bar view background color transparent paddingtop status bar height, covering the picture, and the picture no matter what control.

Android Development-Status bar coloring principle and API version number compatible processing

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.