Android more than 4.4 "immersion" status bar effect of the implementation of the method _android

Source: Internet
Author: User

What is an immersion status bar?

Immersion status bar meaning that the color of the status bar changes with the color of the software, so that the status bar and the software color consistent, immersed in it! When we open an application, it's not as ugly as seeing the application and the black edges of the status bar. Immersed status bar because of its ability to provide users with a good user experience, has been more and more applications have been reflected.

Implementation principle

Since 4.4, the system has added a transparent status bar feature WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
Once you add this property, the content in the layout is DecorView automatically populated in the status bar. All implementations are based on this feature, which means that the status bar is empty by default, and then the developer can customize the view to fill this height.

This attribute is important to be used in the implementation process android:fitsSystemWindows="true" . Meaning: View can adjust its layout according to system Windows (such as status bar, soft keyboard), and if the value is true, the view's properties are adjusted paingding to leave room for system windows ....

So now let's look at the concrete ways to achieve it.


General pages are defined by their own class title bar

Realize

From the implementation of the effect, here is roughly divided into two kinds

1, separate to the status bar coloring

Use this open Source LibrarySystemBarTint

 /**
  * status bar Color Setting method
  * @param context
  * @param color
 /public static void Smarttintmanager context, int color) {
   if (Build.VERSION.SDK_INT >= build.version_codes). KitKat) {
     window window = Context.getwindow ();
     Window.addflags (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
     Create an administrative instance of the status bar
     systembartintmanager Tintmanager = new Systembartintmanager (context);
     Activates the status bar setting
     tintmanager.setstatusbartintenabled (true);
     Tintmanager.setstatusbartintcolor (color);
   }
 

Added in the root layout of the corresponding page, and the android:fitsSystemWindows="true" large background color of the whole cannot be set in the root layout, otherwise the status bar coloring will be overwritten

Call the above method to set the specific color (according to the Open Source Library, where a core class, you can directly copy that class to the project)

This is mainly about the specific principle of implementation

 private void Setupstatusbarview (context context, ViewGroup Decorviewgroup) {
   Mstatusbartintview = new View (context) ;
   Layoutparams params = new Layoutparams (layoutparams.match_parent, Mconfig.getstatusbarheight ());
   params.gravity = Gravity.top;
   if (mnavbaravailable &&!mconfig.isnavigationatbottom ()) {
     Params.rightmargin = Mconfig.getnavigationbarwidth ();
   }
   Mstatusbartintview.setlayoutparams (params);
   Mstatusbartintview.setbackgroundcolor (Default_tint_color);
   Mstatusbartintview.setvisibility (view.gone);
   Decorviewgroup.addview (Mstatusbartintview);
 }

With this code, it's easy to see that by dynamically generating a view, the view width is MATCH_PARENT , the height is the height of the system's status bar, and then a background color is set for the dynamically generated view, and the view is added to decorViewGroup the view container at the end. Then let's see who this view is.

 /**
  * constructor. Call the "in" host activity onCreate how the ITS
  * content view has been set. You are should always create new instances when * The host activity is
  recreated.
  * @param activity of the
  host activity.
  * *
 @TargetApi public
 systembartintmanager (activity activity) {

   Window win = Activity.getwindow ();
   Obtain Decorview root layout container
   viewgroup decorviewgroup = (viewgroup) win.getdecorview (); 
   ..... 
   if (mstatusbaravailable) {
     //This view container is Decorviewgroup
     setupstatusbarview (activity, decorviewgroup);
   }
   if (mnavbaravailable) {
     Setupnavbarview (activity, decorviewgroup);
   }

 }

Note that the above two manually added comments, which is the idea that the status bar is transparent, adding a view to the root layout, decorViewGroup such as the height of the status bar. As for what color you make this view, it's in your mood.

2, with the title bar background color to fill the status bar

When the status bar is set to semitransparent, the problem is that the following content occupies the status bar.

If we add in the root layout of the activityandroid:fitsSystemWindows="true"

Then the status bar is still visible and not occupied. The function of this property is here.

At this point we use the height of the status bar to set a distance for the following content padding-top (because the status bar translucent, the following content will occupy the original status bar, then set it to a padding state bar height) so set, in the background of this view padding , The original status bar height fills the same background color, so it looks like the so-called immersion

In this way, the status bar is translucent, with the following content to fill the appropriate (because the default translucent will be occupied)

The code is as follows:

 @SuppressLint ("Inlinedapi") public static void Setimmerselayout (activity context, View
  View) {if (context = NULL | | view = NULL) {return; } if (Build.VERSION.SDK_INT >= build.version_codes.
    KitKat) {window window = Context.getwindow (); Window.setflags (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT
    _status);
    int statusbarheight = Getstatusbarheight (Context.getbasecontext ());
  View.setpadding (0, statusbarheight, 0, 0); }/** * is used to get the height of the status bar.
 Use the Resource object to get (recommended) * * @return Returns the pixel value of the height of the status bar.
  * * public static int Getstatusbarheight (context context) {int = 0;
  int ResourceID = Context.getresources (). Getidentifier ("Status_bar_height", "Dimen", "Android");
  if (ResourceID > 0) {result = Context.getresources (). Getdimensionpixelsize (ResourceID);
return result; }

To set this view padding_top , which padding is just the height of the status bar, the view background is populated with the status bar.

It is noteworthy: the height of the title bar must be wrap_content , because if the specific height, and then set a paddingtop word, then the contents of the title bar will be squeezed out, incomplete.

So the usual practice is to put the original title bar outside the nested one <FrameLayout /> , and then set the title bar background to <FrameLayout /> the background color

 <framelayout
   android:id= "@+id/title"
   android:layout_width= "match_parent"
   @ Color/common_theme_color "
   android:layout_height=" Wrap_content ">

Summarize

About the immersion effect of the realization of the way to this, I hope the content of this article for everyone's study or work can bring certain help, if there are questions you can also message exchange.

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.