Implementation of the Android standard, fixed and immersed status bar

Source: Internet
Author: User
Tags static class
Today summed up the status bar color settings, common to have a fixed color or immersion implementation, nonsense not to say, directly above the figure.
Standard:
Fixed color:
Immersion type:
My personal latest immersive implementation, but this feature only supports Android4.4 systems.
Here is a specific implementation process.
First of all, the standard is not said, the default is. Then again immersed, the most complex put behind.

Immersion is very simple, two lines of code to fix:

Setcontentview (R.layout.activity_main) in the activity, before adding:

if (Build.VERSION.SDK_INT >= build.version_codes. KitKat) {
Transparent status bar
GetWindow (). Addflags (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
Transparent navigation bar
GetWindow (). Addflags (WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}
Again, fixed color settings, need a dependent Java file, first posted out and so will be used. There is also a point to note, which requires the android:fitssystemwindows= "true" attribute in the outermost layer of the current activity's layout file.

Import Android.annotation.SuppressLint;


Import Android.annotation.TargetApi;


Import android.app.Activity;


Import Android.content.Context;


Import android.content.res.Configuration;


Import android.content.res.Resources;


Import Android.content.res.TypedArray;


Import android.graphics.drawable.Drawable;


Import Android.os.Build;


Import Android.util.DisplayMetrics;


Import Android.util.TypedValue;


Import android.view.Gravity;


Import Android.view.View;


Import android.view.ViewConfiguration;


Import Android.view.ViewGroup;


Import Android.view.Window;


Import Android.view.WindowManager;


Import Android.widget.FrameLayout.LayoutParams;


Import Java.lang.reflect.Method;


@SuppressWarnings ({"Unchecked", "Rawtypes"})


public class Systemstatusmanager


{


Static


{


if (Build.VERSION.SDK_INT >= build.version_codes. KitKat) {


try {


Class C = class.forname ("android.os.SystemProperties");


Method m = C.getdeclaredmethod ("Get", string.class);


M.setaccessible (TRUE);


Snavbaroverride = (String) m.invoke (null, "Qemu.hw.mainkeys");


catch (Throwable e) {


Snavbaroverride = null;


}


}


}


/**


* The default system bar tint color value.


*/


public static final int default_tint_color = 0x99000000;


private static String snavbaroverride;


Private final Systembarconfig Mconfig;


Private Boolean mstatusbaravailable;


Private Boolean mnavbaravailable;


Private Boolean mstatusbartintenabled;


Private Boolean mnavbartintenabled;


Private View Mstatusbartintview;


Private View Mnavbartintview;


/**


* constructor. Call the "in" host activity OnCreate


* Content view has been set. Should always create new instances when


* The host activity is recreated.


*


* @param activity of the host activity.


*/


@TargetApi (19)


Public Systemstatusmanager (activity activity) {


Window win = Activity.getwindow ();


ViewGroup Decorviewgroup = (viewgroup) Win.getdecorview ();


if (Build.VERSION.SDK_INT >= build.version_codes. KitKat) {


Check Theme Attrs


Int[] Attrs = {Android. R.attr.windowtranslucentstatus,


Android. R.attr.windowtranslucentnavigation};


TypedArray a = activity.obtainstyledattributes (attrs);


try {


mstatusbaravailable = A.getboolean (0, false);


mnavbaravailable = A.getboolean (1, false);


finally {


A.recycle ();


}


Check window Flags


Windowmanager.layoutparams winparams = Win.getattributes ();


int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;


if ((Winparams.flags & bits)!= 0) {


Mstatusbaravailable = true;


}


bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION;


if ((Winparams.flags & bits)!= 0) {


Mnavbaravailable = true;


}


}


Mconfig = new Systembarconfig (activity, mstatusbaravailable, mnavbaravailable);


Device might not have virtual navigation keys


if (!mconfig.hasnavigtionbar ()) {


Mnavbaravailable = false;


}


if (mstatusbaravailable) {


Setupstatusbarview (activity, decorviewgroup);


}


if (mnavbaravailable) {


Setupnavbarview (activity, decorviewgroup);


}


}


/**


* Enable Tinting of the system status bar.


*


* If The platform is running Jelly Bean or earlier, or translucent system


* UI modes have not been enabled in either the theme or via window flags,


* Then this method is does nothing.


*


* @param enabled True to enable tinting and false to disable it (default).


*/


public void Setstatusbartintenabled (Boolean enabled) {


mstatusbartintenabled = enabled;


if (mstatusbaravailable) {


Mstatusbartintview.setvisibility (enabled?) View.VISIBLE:View.GONE);


}


}


/**


* Enable tinting of the system navigation bar.


*


* If The platform does not have soft navigation keys, is running Jelly Bean


* or earlier, or translucent system UI modes have not been enabled in either


* The theme or via window flags, then this is does nothing.


*


* @param enabled True to enable tinting and false to disable it (default).


*/


public void Setnavigationbartintenabled (Boolean enabled) {


mnavbartintenabled = enabled;


if (mnavbaravailable) {


Mnavbartintview.setvisibility (enabled?) View.VISIBLE:View.GONE);


}


}


/**


* Apply The specified color tint to all system UI bars.


*


* @param color The color of the background tint.


*/


public void Settintcolor (int color) {


Setstatusbartintcolor (color);


Setnavigationbartintcolor (color);


}


/**


* Apply the specified drawable or color resource to all system UI bars.


*


* @param res The identifier of the resource.


*/


public void Settintresource (int res) {


Setstatusbartintresource (RES);


Setnavigationbartintresource (RES);


}


/**


* Apply The specified drawable to all system UI bars.


*


* @param drawable the "drawable to" as the background, or null to remove it.


*/


public void settintdrawable (drawable drawable) {


Setstatusbartintdrawable (drawable);


Setnavigationbartintdrawable (drawable);


}


/**


* Apply The specified alpha to all system UI bars.


*


* @param alpha-alpha to-use


*/


public void Settintalpha (float alpha) {


Setstatusbaralpha (Alpha);


Setnavigationbaralpha (Alpha);


}


/**


* Apply The specified color tint to the system status bar.


*


* @param color The color of the background tint.


*/


public void Setstatusbartintcolor (int color) {


if (mstatusbaravailable) {


Mstatusbartintview.setbackgroundcolor (color);


}


}


/**


* Apply the specified drawable or color resource to the system status bar.


*


* @param res The identifier of the resource.


*/


public void Setstatusbartintresource (int res) {


if (mstatusbaravailable) {


Mstatusbartintview.setbackgroundresource (RES);


}


}


/**


* Apply The specified drawable to the system status bar.


*


* @param drawable the "drawable to" as the background, or null to remove it.


*/


@SuppressWarnings ("deprecation")


public void setstatusbartintdrawable (drawable drawable) {


if (mstatusbaravailable) {


Mstatusbartintview.setbackgrounddrawable (drawable);


}


}


/**


* Apply The specified alpha to the system status bar.


*


* @param alpha-alpha to-use


*/


@TargetApi (11)


public void Setstatusbaralpha (float alpha) {


if (mstatusbaravailable && Build.VERSION.SDK_INT >= build.version_codes. Honeycomb) {


Mstatusbartintview.setalpha (Alpha);


}


}


/**


* Apply The specified color tint to the system navigation bar.


*


* @param color The color of the background tint.


*/


public void Setnavigationbartintcolor (int color) {


if (mnavbaravailable) {


Mnavbartintview.setbackgroundcolor (color);


}


}


/**


* Apply the specified drawable or color resource to the system navigation bar.


*


* @param res The identifier of the resource.


*/


public void Setnavigationbartintresource (int res) {


if (mnavbaravailable) {


Mnavbartintview.setbackgroundresource (RES);


}


}


/**


* Apply The specified drawable to the system navigation bar.


*


* @param drawable the "drawable to" as the background, or null to remove it.


*/


@SuppressWarnings ("deprecation")


public void setnavigationbartintdrawable (drawable drawable) {


if (mnavbaravailable) {


Mnavbartintview.setbackgrounddrawable (drawable);


}


}


/**


* Apply The specified alpha to the system navigation bar.


*


* @param alpha-alpha to-use


*/


@TargetApi (11)


public void Setnavigationbaralpha (float alpha) {


if (mnavbaravailable && Build.VERSION.SDK_INT >= build.version_codes. Honeycomb) {


Mnavbartintview.setalpha (Alpha);


}


}


/**


* Get the System bar configuration.


*


* @return The System bar configuration for the current device configuration.


*/


Public Systembarconfig GetConfig () {


return mconfig;


}


/**


* is tinting enabled for the System status bar?


*


* @return True If enabled, False otherwise.


*/


public Boolean isstatusbartintenabled () {


return mstatusbartintenabled;


}


/**


* is tinting enabled for the system navigation bar?


*


* @return True If enabled, False otherwise.


*/


public Boolean isnavbartintenabled () {


return mnavbartintenabled;


}


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


}


private void Setupnavbarview (context context, ViewGroup Decorviewgroup) {


Mnavbartintview = new View (context);


Layoutparams params;


if (Mconfig.isnavigationatbottom ()) {


params = new Layoutparams (layoutparams.match_parent, Mconfig.getnavigationbarheight ());


params.gravity = Gravity.bottom;


} else {


params = new Layoutparams (Mconfig.getnavigationbarwidth (), layoutparams.match_parent);


params.gravity = Gravity.right;


}


Mnavbartintview.setlayoutparams (params);


Mnavbartintview.setbackgroundcolor (Default_tint_color);


Mnavbartintview.setvisibility (View.gone);


Decorviewgroup.addview (Mnavbartintview);


}


/**


* Class which describes system bar sizing and other characteristics for the current


* Device configuration.


*


*/


public static class Systembarconfig {


private static final String Status_bar_height_res_name = "Status_bar_height";


private static final String Nav_bar_height_res_name = "Navigation_bar_height";


private static final String Nav_bar_height_landscape_res_name = "Navigation_bar_height_landscape";


private static final String Nav_bar_width_res_name = "Navigation_bar_width";


private static final String Show_nav_bar_res_name = "Config_shownavigationbar";


Private Final Boolean Mtranslucentstatusbar;


Private Final Boolean Mtranslucentnavbar;


private final int mstatusbarheight;


private final int mactionbarheight;


Private Final Boolean Mhasnavigationbar;


private final int mnavigationbarheight;


private final int mnavigationbarwidth;


Private Final Boolean minportrait;


Private final float MSMALLESTWIDTHDP;


Private Systembarconfig (activity activity, Boolean Translucentstatusbar, Boolean Traslucentnavbar) {


Resources res = activity.getresources ();


Minportrait = (res.getconfiguration (). Orientation = = configuration.orientation_portrait);


MSMALLESTWIDTHDP = GETSMALLESTWIDTHDP (activity);


Mstatusbarheight = Getinternaldimensionsize (res, status_bar_height_res_name);


Mactionbarheight = getactionbarheight (activity);


Mnavigationbarheight = getnavigationbarheight (activity);


Mnavigationbarwidth = getnavigationbarwidth (activity);


Mhasnavigationbar = (mnavigationbarheight > 0);


Mtranslucentstatusbar = Translucentstatusbar;


Mtranslucentnavbar = Traslucentnavbar;


}


@TargetApi (14)


private int Getactionbarheight (context context) {


int result = 0;


if (Build.VERSION.SDK_INT >= build.version_codes. Ice_cream_sandwich) {


Typedvalue TV = new Typedvalue ();


Context.gettheme (). Resolveattribute (Android. R.attr.actionbarsize, TV, true);


result = Typedvalue.complextodimensionpixelsize (Tv.data, Context.getresources (). Getdisplaymetrics ());


}


return result;


}


@TargetApi (14)


private int Getnavigationbarheight (context context) {


Resources res = context.getresources ();


int result = 0;


if (Build.VERSION.SDK_INT >= build.version_codes. Ice_cream_sandwich) {


if (Hasnavbar (context)) {


String key;


if (minportrait) {


key = Nav_bar_height_res_name;


} else {


key = Nav_bar_height_landscape_res_name;


}


Return Getinternaldimensionsize (res, key);


}


}


return result;


}


@TargetApi (14)


private int Getnavigationbarwidth (context context) {


Resources res = context.getresources ();


int result = 0;


if (Build.VERSION.SDK_INT >= build.version_codes. Ice_cream_sandwich) {


if (Hasnavbar (context)) {


Return Getinternaldimensionsize (res, nav_bar_width_res_name);


}


}


return result;


}


@TargetApi (14)


Private Boolean Hasnavbar (context context) {


Resources res = context.getresources ();


int ResourceID = Res.getidentifier (show_nav_bar_res_name, "bool", "Android");


if (ResourceID!= 0) {


Boolean Hasnav = Res.getboolean (ResourceID);


Check Override flag


if ("1". Equals (Snavbaroverride)) {


Hasnav = false;


else if ("0". Equals (Snavbaroverride)) {


Hasnav = true;


}


return hasnav;


else {//fallback


Return! Viewconfiguration.get (context). Haspermanentmenukey ();


}


}


private int Getinternaldimensionsize (resources res, String key) {


int result = 0;


int ResourceID = Res.getidentifier (Key, "Dimen", "Android");


if (ResourceID > 0) {


result = Res.getdimensionpixelsize (ResourceID);


}


return result;


}


@SuppressLint ("Newapi")


Private float GETSMALLESTWIDTHDP (activity activity) {


Displaymetrics metrics = new Displaymetrics ();


if (Build.VERSION.SDK_INT >= build.version_codes. Jelly_bean) {


Activity.getwindowmanager (). Getdefaultdisplay (). Getrealmetrics (metrics);


} else {


Activity.getwindowmanager (). Getdefaultdisplay (). Getmetrics (metrics);


}


float WIDTHDP = metrics.widthpixels/metrics.density;


float HEIGHTDP = metrics.heightpixels/metrics.density;


Return Math.min (WIDTHDP, HEIGHTDP);


}


/**


* Should a navigation bar appear at the bottom of the


* Device configuration? A navigation bar may appear in the right side of


* The screen in certain configurations.


*


* @return True If navigation should appear at the bottom of the, False otherwise.


*/


public Boolean isnavigationatbottom () {


Return (MSMALLESTWIDTHDP >= | | minportrait);


}


/**


* Get the height of the system status bar.


*


* @return The height of the status bar (in pixels).


*/


public int getstatusbarheight () {


return mstatusbarheight;


}


/**


* Get the height of the action bar.


*


* @return The height of the action bar (in pixels).


*/


public int getactionbarheight () {


return mactionbarheight;


}


/**


* Does this device have a system navigation bar?


*


* @return True If this device uses soft key navigation, False otherwise.


*/


public Boolean Hasnavigtionbar () {


return mhasnavigationbar;


}


/**


* Get the height of the system navigation bar.


*


* @return The height of the navigation bar (in pixels). If the device does not have


* Soft navigation keys, this'll always return 0.


*/


public int getnavigationbarheight () {


return mnavigationbarheight;


}


/**


* Get the width of the ' System navigation bar ' when it's placed vertically on the screen.


*


* @return The width of the navigation bar (in pixels). If the device does not have


* Soft navigation keys, this'll always return 0.


*/


public int getnavigationbarwidth () {


return mnavigationbarwidth;


}


/**


* Get the layout inset the to any system UI that is appears at the top of the screen.


*


* @param withactionbar True to include the height of the action bar, False otherwise.


* @return The layout inset (in pixels).


*/


public int Getpixelinsettop (Boolean withactionbar) {


Return (Mtranslucentstatusbar mstatusbarheight:0) + (Withactionbar? mactionbarheight:0);


}


/**


* Get the layout inset the to any system UI which is appears at the bottom of the.


*


* @return The layout inset (in pixels).


*/


public int Getpixelinsetbottom () {


if (Mtranslucentnavbar && isnavigationatbottom ()) {


return mnavigationbarheight;


} else {


return 0;


}


}


/**


* Get the layout inset the ' any ' system UI that is appears at ' right ' screen.


*


* @return The layout inset (in pixels).


*/


public int getpixelinsetright () {


if (Mtranslucentnavbar &&!isnavigationatbottom ()) {


return mnavigationbarwidth;


} else {


return 0;


}


}


}


}


OK, then I'll post the main activity.

public class Mainactivity extends activity {


@Override


protected void OnCreate (Bundle savedinstancestate) {


Super.oncreate (savedinstancestate);


Settranslucentstatus ();


Setcontentview (R.layout.activity_main);


}


private void Settranslucentstatus () {


if (Build.VERSION.SDK_INT >= build.version_codes. KitKat) {


Transparent status bar


GetWindow (). Addflags (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);


Transparent navigation bar


GetWindow (). Addflags (WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);


Systemstatusmanager Tintmanager = new Systemstatusmanager (this);


Tintmanager.setstatusbartintenabled (TRUE);


Tintmanager.setstatusbartintresource (r.color.red);//The color you want to set


GetWindow (). Getdecorview (). Setfitssystemwindows (True);


}


}


}


OK, this is the second of the above effect graph, the fixed status bar color.

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.