Home>

This article describes an example of android development to modify the background color and icon color of the status bar.Share with everyone,details as follows:

Modify the status bar background and icon colors

The default is white on a black background.It ’s going to be changed to black on white

1. The background of the status bar is white:set in style

<item name="colorprimarydark">@ color/white</item>

2. Write and modify the color of the status bar icon (only black and white are known for the time being)

public class statusbarutil {
/**
 * Modify the status bar to be completely transparent
 * @param activity
 * /
@targetapi (19)
public static void transparencybar (activity activity) {
 if (build.version.sdk_int>= build.version_codes.lollipop) {
 window window=activity.getwindow ();
 window.clearflags (windowmanager.layoutparams.flag_translucent_status);
 window.getdecorview (). setsystemuivisibility (view.system_ui_flag_layout_fullscreen
  view.system_ui_flag_layout_stable);
 window.addflags (windowmanager.layoutparams.flag_draws_system_bar_backgrounds);
 window.setstatusbarcolor (color.transparent);
 } else
 if (build.version.sdk_int>= build.version_codes.kitkat) {
 window window=activity.getwindow ();
 window.setflags (windowmanager.layoutparams.flag_translucent_status,  windowmanager.layoutparams.flag_translucent_status);
 }
}
/**
 * Modify the status bar color,Support 4.4 and above
 * @param activity
 * @param colorid
 * /
public static void setstatusbarcolor (activity activity, int colorid) {
 if (build.version.sdk_int>= build.version_codes.lollipop) {
 window window=activity.getwindow ();
  //window.addflags(windowmanager.layoutparams.flag_draws_system_bar_backgrounds);
 //window.getdecorview (). setsystemuivisibility (view.system_ui_flag_light_status_bar);
 window.setstatusbarcolor (activity.getresources (). getcolor (colorid));
 } else if (build.version.sdk_int>= build.version_codes.kitkat) {
 //Use the systembartint library to make the 4.4 status bar color,Need to set the status bar to transparent first
 transparencybar (activity);
 systembartintmanager tintmanager=new systembartintmanager (activity);
 tintmanager.setstatusbartintenabled (true);
 tintmanager.setstatusbartintresource (colorid);
 }
}
/**
 * Status bar light color mode,Set black text and icons in the status bar.
 * Compatible with Miuiv 4.4, Flyme and other Android versions 6.0 and above
 * @param activity
 * @return 1:miuui 2:flyme 3:android6.0
 * /
public static int statusbarlightmode (activity activity) {
 int result=0;
 if (build.version.sdk_int>= build.version_codes.kitkat) {
 if (miuisetstatusbarlightmode (activity, true)) {
  result=1;
 } else if (flymesetstatusbarlightmode (activity.getwindow (), true)) {
  result=2;
 } else {
  activity.getwindow (). getdecorview (). setsystemuivisibility (view.system_ui_flag_layout_fullscreen | view.system_ui_flag_light_status_bar);
  result=3;
 }
 }
 return result;
}
/**
 * Set status bar icon to dark and Meizu specific text style
 * Can be used to determine if it is a flyme user
 * @param window The window to be set
 * @param dark Whether to set the status bar text and icon color to dark
 * @return boolean returns true if successful
 *
 * /
public static boolean flymesetstatusbarlightmode (window window, boolean dark) {
 boolean result=false;
 if (window!=null) {
 try {
  windowmanager.layoutparams lp=window.getattributes ();
  field darkflag=windowmanager.layoutparams.class
   .getdeclaredfield ("meizu_flag_dark_status_bar_icon");
  field meizuflags=windowmanager.layoutparams.class
   .getdeclaredfield ("meizuflags");
  darkflag.setaccessible (true);
  meizuflags.setaccessible (true);
  int bit=darkflag.getint (null);
  int value=meizuflags.getint (lp);
  if (dark) {
  value |=bit;
  } else {
  value&= ~ bit;
  }
  meizuflags.setint (lp, value);
  window.setattributes (lp);
  result=true;
 } catch (exception e) {
 }
 }
 return result;
}
/**
 * Requires miuiv6 or higher
 * @param activity
 * @param dark Whether to set the status bar text and icon color to dark
 * @return boolean returns true if successful
 *
 * /
public static boolean miuisetstatusbarlightmode (activity activity, boolean dark) {
 boolean result=false;
 window window=activity.getwindow ();
 if (window!=null) {
 class clazz=window.getclass ();
 try {
  int darkmodeflag=0;
  class layoutparams=class.forname ("android.view.miuiwindowmanager $layoutparams");
  field field=layoutparams.getfield ("extra_flag_status_bar_dark_mode");
  darkmodeflag=field.getint (layoutparams);
  method extraflagfield=clazz.getmethod ("setextraflags", int.class, int.class);
  if (dark) {
  extraflagfield.invoke (window, darkmodeflag, darkmodeflag);//The status bar is transparent and black font
  } else {
  extraflagfield.invoke (window, 0, darkmodeflag);//clear black font
  }
  result=true;
  if (build.version.sdk_int>= build.version_codes.m) {
  //Development version 7.7.13 and later versions use the system API, the old method is invalid but no error is reported,So both methods must be added
  if (dark) {
   activity.getwindow (). getdecorview (). setsystemuivisibility (view.system_ui_flag_layout_fullscreen | view.system_ui_flag_light_status_bar);
  } else {
   activity.getwindow (). getdecorview (). setsystemuivisibility (view.system_ui_flag_visible);
  }
  }
 } catch (exception e) {
 }
 }
 return result;
}}

The above code is/go.php?id=125520&s=aFound on it, you can go to see

3, the specific reference Liezi in baseactivity

@override
protected void oncreate (@nullable bundle savedinstancestate) {
 super.oncreate (savedinstancestate);
 activityutils.add (this, getclass ());
 mcontext=this;
 statusbarutil.statusbarlightmode (this);
}

4.The normal status bar has changed

The status bar is changed,But you will see that the entire activity layout will move up and fill the entire screen

Solution 1:add apptheme in style

<item name="android:fitssystemwindows">true</item>

If you add the above code, the layout is moved down without affecting other things.Then don't look down

android:fitssystemwindows is a pit, there are problems with the style of many popups

Solution 2:Add paddingtop for each layout yourself

libutils:

/**
 * Get status bar height
 * @return
 * /
public static int getstatusbarheight (context context) {
 int result=0;
 int resourceid=context.getresources (). getidentifier ("status_bar_height", "dimen", "android");
 if (resourceid>0) {
  result=context.getresources (). getdimensionpixelsize (resourceid);
 }
 return result;
}
//Set layout height from status bar
public static void setlayoutpadding (activity activity, view contentlayout) {
 if (build.version.sdk_int>= build.version_codes.kitkat) {
  contentlayout
    .setpadding (contentlayout.getpaddingleft (), getstatusbarheight (activity) + contentlayout.getpaddingtop (),      contentlayout.getpaddingright (), contentlayout.getpaddingbottom ());
 }
}

References:

protected void oncreate (@nonnull bundle savedinstancestate, int resid, int titleid) {
 super.oncreate (savedinstancestate);
 mcontext=this;
 setcontentview (r.layout.activity_base);
 statusbarutil.statusbarlightmode (this);
 libutils.setlayoutpadding (this, ((viewgroup) findviewbyid (android.r.id.content)). getchildat (0));}

Note:The call to libutils.setlayoutpadding must be done after setcontentview, and android.r.id.content is the root layout for each layout.Do not understand yourself Baidu

Also consider the issue of android version,Generally the system under 5.0 still uses the default

  • Previous Java sample code using es query
  • Next androidstudio30 using butterknife error reporting solution
  • Trends