Android immersive status bar Raiders

Source: Internet
Author: User

Objective

There is no discussion about [immersive] This word is used well, we can understand it. This article is mainly my experience in the actual project, sorted out and share with you, welcome to explore. Because the internship has been 996, no time to summarize, today suddenly think that this kind of work let me forget the life, it is time to do a break. Writing this article is already 23:44, too late to post some demo, but here the code is the previous project picked out, is able to execute, but I do not really do it again. Note that all the code is only valid on Android 4.4 and above.

Consider

Depending on the actual project, the immersive implementation strategy that may be selected will also be different.

Traditional solid Color Actionbar

If your app uses actionbar that follow Android specifications or has a solid-color layout at the top of the screen, there's an intrusion-less scheme that is implemented as an open source project Systembartint.
Let your activity inherit a baseactivity, rewrite it in baseactivity onpostcreate

    @Override    protectedvoidonPostCreate(Bundle savedInstanceState) {        super.onPostCreate(savedInstanceState);        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)  {        ImmerseHelper.setSystemBarTransparent(this);        }    }

Focus on Immersehelper This class, the first post code to explain.

 Public  class immersehelper {    @TargetApi(Build.version_codes. KITKAT) Public Static void setsystembartransparent(Activity paramactivity)        {window window = Paramactivity.getwindow ();        Windowmanager.layoutparams layoutparams = Window.getattributes ();        Layoutparams.flags |= WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;        Window.setattributes (Layoutparams);        Hackstatusbartransparent (paramactivity);    setContentPadding (paramactivity); } Public Static void hackstatusbartransparent(Activity paramactivity) {ViewGroup Localviewgroup = (viewgroup) Paramactivity.getwindow (). Getdecorview (). Findviewbyid (Android Oid.        R.id.content); View Colorview =NewView (paramactivity);        Colorview.setbackgroundresource (R.color.statusbar_color); Localviewgroup.addview (Colorview, ViewGroup.LayoutParams.MATCH_PARENT, immersehelper.getstatusbarheight (par    amactivity)); } Public Static void setcontentpadding(Activity activity) {(ViewGroup) Activity.findviewbyid (Android. r.id.content)). Getchildat (0). Setpadding (0, Immersehelper.getstatusbarheight (activity) + immersehelper.getactionbarheight (activity),0,0); }/** * Get status bar height, unit px * @param context * @return  */     Public Static int Getstatusbarheight(Context context) {intresult =0;intResourceId = Context.getresources (). Getidentifier ("Status_bar_height","Dimen","Android");if(ResourceId >0{result = Context.getresources (). Getdimensionpixelsize (ResourceId); }returnResult }/** * Get actionbar height, Unit px * @param context * @return  */     Public Static int Getactionbarheight(Context context) {Typedvalue Localtypedvalue =NewTypedvalue ();if(Context.gettheme (). Resolveattribute (Android. R.attr.actionbarsize, Localtypedvalue,true)) {returnTypedvalue.complextodimensionpixelsize (Localtypedvalue.data, Context.getresources (). GetDisplayMetrics ()); }return 0; }}

The simple thing is postCreate to tamper with the activity at the time, so that other developers in the group almost don't feel the change, just need to designate the base class of activity as BaseActivity good
ImmerseHelperThis class has done three things.
-Opens the Flag_translucent_status Sign of window
-Added a solid color block to a view that is exactly the same size as the status bar
-Set Paddingtop to the activity's root view
Flag_translucent_status This flag is open, the status bar is transparent, at the same time, the main layout of our activity, that is, the layout of the Setcontentview incoming, will be top of the screen, is covered by the status bar part, Note that Actionbar is not affected. So we set the paddingtop of the activity's root view in the third step, the height of the status bar plus the height of the Actionbar, so that the contents of the activity will return to the previous normal position. But at this point the status bar is transparent, so we added a view to the same size as the status bar, the color and actionbar consistent, so there is an immersive effect.
Of course, the effect here is the status bar below a layer of translucent black, then we join the view, so do not worry about the status of the text, in 4.4 can only do this effect, 5.0 can make the bottom of the status bar completely transparent, this later said.
It's just that the light does not work, and you need to know why we do it.

Principle

First look at the hierarchy view

The Getdecorview we get is the leftmost decorview, and Setcontentview is the Id/content view's direct sub view, and the Id/content peer view is actionbar.
activity.getWindow().getDecorView().findViewById(android.R.id.content);This sentence to get is id/content this view, we add a solid color view, because opened the flag_translucent_status, this solid color view directly on the top, that is, the status bar covers the place.
Systembartint is not in the AddView to the id/content, but directly to getDecorView() the AddView, and Swipebacklayout is in Decorview and his child view between the insertion of their own layout, The equivalent of hijacking Decorview's child view, so assume that the same time using the two open source projects without change, or swipe back to the row is the status bar, or the status bar torn. If I write like this, I will not clash with Swipebacklayout.

Non-Traditional

Assuming you want to do this, the picture is at the top, then it is not recommended to use Actionbar, and at the same time it does not need to setcontentpadding this step. Sometimes this will lead to the content of some pages over-biased, this time suggested with a dimen, under normal circumstances is 0DP, v19 and above is 24DP, which is the height of the status bar, which pages to separate the status bar, with this dimen do margintop, Or include a height for the layout of this dimen.
This kind of invasion is stronger, do a new page need to always pay attention to the status bar and whether it is necessary to keep distance, very easy to forget, but also not difficult, after all, a implementation on the look out.

Lolipop+ Unique Methods

This is the API level 19 method, the only drawback is that the status bar is not completely transparent, but the bottom has a translucent black bar, at API 21, we can remove this translucent black bar, so that the status bar is completely transparent.
onCreateafter the Setcontentview call, the activity will be passed to the following method, you can let your app on the API level 21 has a completely transparent status bar, the same time on the API 19 to use the above implementation

    @TargetApi(Build.version_codes. LOLLIPOP) Public void setsystembartransparent(Activity paramactivity) {if(Shouldusetransparentsystembar ())            {window window = Paramactivity.getwindow (); Windowmanager.layoutparams layoutparams = Window.getattributes ();if(Build.VERSION.SDK_INT >= build.version_codes. LOLLIPOP) {//api 21 SolutionsView Systemdecor = Window.getdecorview (); Systemdecor.setsystemuivisibility (View.system_ui_flag_layout_fullscreen |                view.system_ui_flag_layout_stable);                Layoutparams.flags |= WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS; Window.setstatusbarcolor (0x00000000); }Else{//api 19 SolutionsLayoutparams.flags |= WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;        } window.setattributes (Layoutparams); }    }

The solution of API 21 is theoretically able to complete with XML, but I actually found that it is not possible, only the practical code is valid.
Let's say you want to ask where API 20 goes, and see what is written in the parentheses of API 20 inside SDK manager.

Android immersive status bar Raiders

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.