Android immersive status bar Raiders

Source: Internet
Author: User

Objective

There is no discussion about the use of the term [immersive], which can be understood by all. 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, there is no time to summarize, today suddenly feel like 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 once the project picked out, is can run, 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 is using an Android-compliant Actionbar or has a solid-color layout at the top of the screen, there's an intrusion-less scenario, which is implemented as a reference to the 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, first put code to explain again.

 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; }}

In short, it is the postCreate time to tamper with the activity so that other developers in the group can hardly feel the change, just specify 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, and 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 add a color to a view that is exactly the same size as the status bar, and the color is consistent with the actionbar, so there is an immersive effect.
Of course, the effect here is the status bar below a layer of translucent black, and then we add the view, so do not worry about not see the status of text, in 4.4 can only do this effect, 5.0 on the bottom of the status bar to be completely transparent, this later said.
But 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 the open flag_translucent_status, this solid color view directly on the top, that is, the status bar covers the place.
The systembartint is not a addview to the id/content, but a direct addview to the getDecorView() middle, while Swipebacklayout inserts his own layout between Decorview and his child view, The equivalent of hijacking Decorview's child view, so if you use both open source projects without modification, either swipe back to go to the status bar, or the status bar torn. If I write like this, I will not clash with Swipebacklayout.

Non-Traditional

If you want to do this, the picture is completely top-up, then it is not recommended to use Actionbar, and you do 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.
Such a strong invasion, do a new page need to always pay attention to the status bar and whether the need to keep distance, it is easy to forget, but it is not difficult, after all, a run 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 completely transparent.
onCreateafter the Setcontentview call, the activity parameters are passed to the following method, you can let your app on the API level 21 has a fully transparent status bar, while 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 for API 21 can theoretically be done in XML, but my actual test findings are not, only valid with code.
If you want to ask where API 20 is going, check out what is written in the parentheses of API 20 in SDK Manager.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android immersive status bar Raiders

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.