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

Source: Internet
Author: User
Tags getcolor

Introduced

First on the actual, there are three versions please note that the API version is differentiated

api>=20 api=19 api<19

The above effect is what we call the coloring of the status bar, not the immersive status bar, which is something else.
To achieve the above effect is not difficult, read this article to understand the principle, you will feel like the above effect is really simple.
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 of 19, which is the only transparent status bar implemented above api>=19, which is why the API is differentiated above.

Note: The following style style is the same as the code, but with 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 to prevent overlapping
There are two ways to resolve this:

    1. Set in XML: android:fitsSystemWindows="true" automatically finds the outermost view setting for a view setup (api+14) system layout Paddingtop

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

Setting properties directly in XML is simple, but has limitations, only valid for the outermost view, if it is set fitssystemwindows in the view view of fragment and then added to the screen is not valid.
Code control, to solve the problem of fragment, but to 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, as well as the settings padding sometimes fail, you need handler.post(new Runnable()) to ensure code execution

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 execute
    • API=19: Black and gray progressive transparent status bar
    • API>=20: The system automatically generates a translucent status bar

So there are two ways to set the color of the reserved space created in the second step:

    1. Add View: The parent container for the entire view framelayout (id=android. R.id.content) Add a status bar with a color view that overrides the status bar.
    2. Auto Render: Sets the background color for the Paddingtop control, and when the control is padding processed, the color is also rendered to the status bar.
Add the code that overrides the view of the status bar:
    /** * Set the status bar color for the app main color * Mate {@link #setTranslucentWindows (Activity)} method Use the * Main method to add a view and set the background color 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

The implementation of the status bar coloring is not complicated, only three steps, the real use is actually a few lines of code or several method calls. But in actual use needs to consider the actual demand, the compatibility question. 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 does not have any control.

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

Related Article

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.