Android realizes immersive navigation bar instance code _android

Source: Internet
Author: User
Tags static class

Nonsense not much said, directly to everyone to paste code, the specific code as follows:

Private Systembartintmanager Tintmanager;
@Override
protected void onCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.activity_main);
GetWindow (). Addflags (WindowManager.LayoutParams.FLAG_FULLSCREEN);//Full Screen
Initwindow ();
}
private void Initwindow () {
if (Build.VERSION.SDK_INT >= build.version_codes. KitKat) {
GetWindow (). Addflags (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
GetWindow (). Addflags (WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
Tintmanager = new Systembartintmanager (this);
Tintmanager.setstatusbartintcolor (This.getresources (). GetColor (R.color.blue)); Sets the status bar color
tintmanager.setstatusbartintenabled (true); 
}
Package zcd.libs;
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;
/** * Class to manage the status and navigation bar tint effects when using KitKat * Translucent system UI modes. * */public class Systembartintmanager {static {//Android allows a system property to override the presence of the Navi
Gation Bar. if (Build.VERSION.SDK_INT >= build.version_codes. KitKat) {try {Classc = 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 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 (); 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 (Mstatus baravailable) {Setupstatusbarview (activity, decorviewgroup);} if (Mnavbaravailable) {Setupnavbarview (activity, Decorviewgroup);}}
/** * Enable Tinting of the system status bar.  * * If the platform is running Jelly beans 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 setstatusbartintenabled (Boolean enabled) {mstatusbartintenabled = enabled; if (mstatusbaravailable) {Msta Tusbartintview.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 is enabled in either * the theme or via window flags, and then this method 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); Setnavigationbaralp Ha (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 (one) public void Setstatusbaralpha (float alpha) {if mstatusbaravailable & Amp;& 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 (one) public void Setnavigationbaralpha (float alpha) {if mnavbaravailable &&am P 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;}/** * are tinting enabled for the system Navig
ation bar?
* * @return True if enabled, False otherwise. * * Public boolean isnavbartintenabled () {return mnavbartintenabled;} private void Setupstatusbarview (context, Vi
Ewgroup 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"; p
Rivate 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 = ACTI
Vity.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 private int Getactionbarheight (context context) {int = 0; if (Build.VERSION.SDK_INT >= BUILD.V Ersion_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 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 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) {return getinternaldimensionsize (res, nav_bar_width_res_name);}
Result @TargetApi 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 (the "" = Static Block) if ("1". Equa
LS (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 = 0; int ResourceID = Res.getidentifier (k
EY, "Dimen", "Android");
if (ResourceID > 0) {result = Res.getdimensionpixelsize (ResourceID); @SuppressLint ("Newapi") private float GETSMALLESTWIDTHDP (activity activity) {displaymetrics metrics = new Displaymetri
CS (); if (Build.VERSION.SDK_INT >= build.version_codes. Jelly_bean) {Activity.getwindowmanager (). Getdefaultdisplay (). Getrealmetrics (metrics);} else {//TODO this are not Corre
CT, but we don ' t really care Pre-kitkat 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 ' screen in ' current * device configuration? A Navigation baR may appear on 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 T
He 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 th
E 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 to any system UI that AP
Pears 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) + (withactio Nbar?
mactionbarheight:0);
The/** * Get the layout inset to the any system UI this appears at the bottom of the.
* * @return The layout inset (in pixels). */public int getpixelinsetbottom () {if (Mtranslucentnavbar && isnavigationatbottom ()) {return mnavigationbarhe
ight;
else {return 0;}} /** * Get the LayOut inset the to any system UI that is appears at the right of the.
* * @return The layout inset (in pixels). */public int getpixelinsetright () {if (Mtranslucentnavbar &&!isnavigationatbottom ()) {return Mnavigationbarwi
Dth
else {return 0;}} }
}

The above code is small to introduce you to the Android implementation of immersive navigation bar instance code, the code is easy to understand, all did not give everybody to attach the annotation, everybody in the reference process discovered that has the question, welcome to my message, small series will promptly reply everybody, here also very thanks everybody to the cloud Habitat Community website support!

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.