android 沉浸式之改变小米状态栏颜色

这个是基于SystemBarTintManager更改的

增加一个方法:用于更改MIUIV6系统上的状态栏字体颜色 ,目前我仅仅只发现MIUIV6上可以更改,在android5.0上以及其它4.4以上系统没有发现可以更改字体颜色的代码


核心代码:

  1. public void setStatusBarDarkMode(boolean darkmode, Activity activity) {  
  2.          if (sIsMiuiV6) {  
  3.              Class<? extends Window> clazz = activity.getWindow().getClass();  
  4.              try {  
  5.              int darkModeFlag = 0;  
  6.              Class<?> layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");  
  7.              Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");  
  8.              darkModeFlag = field.getInt(layoutParams);  
  9.              Method extraFlagField = clazz.getMethod("setExtraFlags"int.classint.class);  
  10.              extraFlagField.invoke(activity.getWindow(), darkmode ? darkModeFlag : 0, darkModeFlag);  
  11.              } catch (Exception e) {  
  12.                  e.printStackTrace();  
  13.              }  
  14.          }  
  15.     }  

全部代码:


  1. /* 
  2.  * Copyright (C) 2013 readyState Software Ltd 
  3.  * 
  4.  * Licensed under the Apache License, Version 2.0 (the "License"); 
  5.  * you may not use this file except in compliance with the License. 
  6.  * You may obtain a copy of the License at 
  7.  * 
  8.  *      http://www.apache.org/licenses/LICENSE-2.0 
  9.  * 
  10.  * Unless required by applicable law or agreed to in writing, software 
  11.  * distributed under the License is distributed on an "AS IS" BASIS, 
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
  13.  * See the License for the specific language governing permissions and 
  14.  * limitations under the License. 
  15.  */  
  16. import android.annotation.SuppressLint;  
  17. import android.annotation.TargetApi;  
  18. import android.app.Activity;  
  19. import android.content.Context;  
  20. import android.content.res.Configuration;  
  21. import android.content.res.Resources;  
  22. import android.content.res.TypedArray;  
  23. import android.graphics.drawable.Drawable;  
  24. import android.os.Build;  
  25. import android.util.DisplayMetrics;  
  26. import android.util.TypedValue;  
  27. import android.view.Gravity;  
  28. import android.view.View;  
  29. import android.view.ViewConfiguration;  
  30. import android.view.ViewGroup;  
  31. import android.view.Window;  
  32. import android.view.WindowManager;  
  33. import android.widget.FrameLayout.LayoutParams;  
  34.   
  35. import java.lang.reflect.Field;  
  36. import java.lang.reflect.Method;  
  37.   
  38. /** 
  39.  * Class to manage status and navigation bar tint effects when using KitKat  
  40.  * translucent system UI modes. 
  41.  * 
  42.  */  
  43. public class SystemBarTintManager {  
  44.       
  45.     /** 
  46.      * The default system bar tint color value. 
  47.      */  
  48.     public static final int DEFAULT_TINT_COLOR = 0x99000000;  
  49.   
  50.     private static String sNavBarOverride;  
  51.   
  52.     private final SystemBarConfig mConfig;  
  53.     private boolean mStatusBarAvailable;  
  54.     private boolean mNavBarAvailable;  
  55.     private boolean mStatusBarTintEnabled;  
  56.     private boolean mNavBarTintEnabled;  
  57.     private View mStatusBarTintView;  
  58.     private View mNavBarTintView;  
  59.     private static boolean sIsMiuiV6;  
  60.     static {  
  61.         // Android allows a system property to override the presence of the navigation bar.  
  62.         // Used by the emulator.  
  63.         // See https://github.com/android/platform_frameworks_base/blob/master/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java#L1076  
  64.         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {  
  65.             try {  
  66.                 Class c = Class.forName("android.os.SystemProperties");  
  67.                 Method m = c.getDeclaredMethod("get", String.class);  
  68.                 m.setAccessible(true);  
  69.                 sIsMiuiV6 = "V6".equals((String) m.invoke(c, "ro.miui.ui.version.name"));  
  70.                 sNavBarOverride = (String) m.invoke(null"qemu.hw.mainkeys");  
  71.             } catch (Throwable e) {  
  72.                 sNavBarOverride = null;  
  73.             }  
  74.         }  
  75.     }  
  76.     /** 
  77.      * Constructor. Call this in the host activity onCreate method after its 
  78.      * content view has been set. You should always create new instances when 
  79.      * the host activity is recreated. 
  80.      * 
  81.      * @param activity The host activity. 
  82.      */  
  83.     @TargetApi(19)  
  84.     public SystemBarTintManager(Activity activity) {  
  85.   
  86.         Window win = activity.getWindow();  
  87.         ViewGroup decorViewGroup = (ViewGroup) win.getDecorView();  
  88.   
  89.         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {  
  90.             // check theme attrs  
  91.             int[] attrs = {android.R.attr.windowTranslucentStatus,  
  92.                     android.R.attr.windowTranslucentNavigation};  
  93.             TypedArray a = activity.obtainStyledAttributes(attrs);  
  94.             try {  
  95.                 mStatusBarAvailable = a.getBoolean(0false);  
  96.                 mNavBarAvailable = a.getBoolean(1false);  
  97.             } finally {  
  98.                 a.recycle();  
  99.             }  
  100.   
  101.             // check window flags  
  102.             WindowManager.LayoutParams winParams = win.getAttributes();  
  103.             int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;  
  104.             if ((winParams.flags & bits) != 0) {  
  105.                 mStatusBarAvailable = true;  
  106.             }  
  107.             bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION;  
  108.             if ((winParams.flags & bits) != 0) {  
  109.                 mNavBarAvailable = true;  
  110.             }  
  111.         }  
  112.   
  113.         mConfig = new SystemBarConfig(activity, mStatusBarAvailable, mNavBarAvailable);  
  114.         // device might not have virtual navigation keys  
  115.         if (!mConfig.hasNavigtionBar()) {  
  116.             mNavBarAvailable = false;  
  117.         }  
  118.   
  119.         if (mStatusBarAvailable) {  
  120.             setupStatusBarView(activity, decorViewGroup);  
  121.         }  
  122.         if (mNavBarAvailable) {  
  123.             setupNavBarView(activity, decorViewGroup);  
  124.         }  
  125.   
  126.     }  
  127.   
  128.     /** 
  129.      * Enable tinting of the system status bar. 
  130.      * 
  131.      * If the platform is running Jelly Bean or earlier, or translucent system 
  132.      * UI modes have not been enabled in either the theme or via window flags, 
  133.      * then this method does nothing. 
  134.      * 
  135.      * @param enabled True to enable tinting, false to disable it (default). 
  136.      */  
  137.     public void setStatusBarTintEnabled(boolean enabled) {  
  138.         mStatusBarTintEnabled = enabled;  
  139.         if (mStatusBarAvailable) {  
  140.             mStatusBarTintView.setVisibility(enabled ? View.VISIBLE : View.GONE);  
  141.         }  
  142.     }  
  143.   
  144.     /** 
  145.      * Enable tinting of the system navigation bar. 
  146.      * 
  147.      * If the platform does not have soft navigation keys, is running Jelly Bean 
  148.      * or earlier, or translucent system UI modes have not been enabled in either 
  149.      * the theme or via window flags, then this method does nothing. 
  150.      * 
  151.      * @param enabled True to enable tinting, false to disable it (default). 
  152.      */  
  153.     public void setNavigationBarTintEnabled(boolean enabled) {  
  154.         mNavBarTintEnabled = enabled;  
  155.         if (mNavBarAvailable) {  
  156.             mNavBarTintView.setVisibility(enabled ? View.VISIBLE : View.GONE);  
  157.         }  
  158.     }  
  159.   
  160.     /** 
  161.      * Apply the specified color tint to all system UI bars. 
  162.      * 
  163.      * @param color The color of the background tint. 
  164.      */  
  165.     public void setTintColor(int color) {  
  166.         setStatusBarTintColor(color);  
  167.         setNavigationBarTintColor(color);  
  168.     }  
  169.   
  170.     /** 
  171.      * Apply the specified drawable or color resource to all system UI bars. 
  172.      * 
  173.      * @param res The identifier of the resource. 
  174.      */  
  175.     public void setTintResource(int res) {  
  176.         setStatusBarTintResource(res);  
  177.         setNavigationBarTintResource(res);  
  178.     }  
  179.   
  180.     /** 
  181.      * Apply the specified drawable to all system UI bars. 
  182.      * 
  183.      * @param drawable The drawable to use as the background, or null to remove it. 
  184.      */  
  185.     public void setTintDrawable(Drawable drawable) {  
  186.         setStatusBarTintDrawable(drawable);  
  187.         setNavigationBarTintDrawable(drawable);  
  188.     }  
  189.   
  190.     /** 
  191.      * Apply the specified alpha to all system UI bars. 
  192.      * 
  193.      * @param alpha The alpha to use 
  194.      */  
  195.     public void setTintAlpha(float alpha) {  
  196.         setStatusBarAlpha(alpha);  
  197.         setNavigationBarAlpha(alpha);  
  198.     }  
  199.   
  200.     /** 
  201.      * Apply the specified color tint to the system status bar. 
  202.      * 
  203.      * @param color The color of the background tint. 
  204.      */  
  205.     public void setStatusBarTintColor(int color) {  
  206.         if (mStatusBarAvailable) {  
  207.             mStatusBarTintView.setBackgroundColor(color);  
  208.         }  
  209.     }  
  210.   
  211.     /** 
  212.      * Apply the specified drawable or color resource to the system status bar. 
  213.      * 
  214.      * @param res The identifier of the resource. 
  215.      */  
  216.     public void setStatusBarTintResource(int res) {  
  217.         if (mStatusBarAvailable) {  
  218.             mStatusBarTintView.setBackgroundResource(res);  
  219.         }  
  220.     }  
  221.   
  222.     /** 
  223.      * Apply the specified drawable to the system status bar. 
  224.      * 
  225.      * @param drawable The drawable to use as the background, or null to remove it. 
  226.      */  
  227.     @SuppressWarnings("deprecation")  
  228.     public void setStatusBarTintDrawable(Drawable drawable) {  
  229.         if (mStatusBarAvailable) {  
  230.             mStatusBarTintView.setBackgroundDrawable(drawable);  
  231.         }  
  232.     }  
  233.   
  234.     /** 
  235.      * Apply the specified alpha to the system status bar. 
  236.      * 
  237.      * @param alpha The alpha to use 
  238.      */  
  239.     @TargetApi(11)  
  240.     public void setStatusBarAlpha(float alpha) {  
  241.         if (mStatusBarAvailable && Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {  
  242.             mStatusBarTintView.setAlpha(alpha);  
  243.         }  
  244.     }  
  245.   
  246.     /** 
  247.      * Apply the specified color tint to the system navigation bar. 
  248.      * 
  249.      * @param color The color of the background tint. 
  250.      */  
  251.     public void setNavigationBarTintColor(int color) {  
  252.         if (mNavBarAvailable) {  
  253.             mNavBarTintView.setBackgroundColor(color);  
  254.         }  
  255.     }  
  256.   
  257.     /** 
  258.      * Apply the specified drawable or color resource to the system navigation bar. 
  259.      * 
  260.      * @param res The identifier of the resource. 
  261.      */  
  262.     public void setNavigationBarTintResource(int res) {  
  263.         if (mNavBarAvailable) {  
  264.             mNavBarTintView.setBackgroundResource(res);  
  265.         }  
  266.     }  
  267.   
  268.     /** 
  269.      * Apply the specified drawable to the system navigation bar. 
  270.      * 
  271.      * @param drawable The drawable to use as the background, or null to remove it. 
  272.      */  
  273.     @SuppressWarnings("deprecation")  
  274.     public void setNavigationBarTintDrawable(Drawable drawable) {  
  275.         if (mNavBarAvailable) {  
  276.             mNavBarTintView.setBackgroundDrawable(drawable);  
  277.         }  
  278.     }  
  279.   
  280.     /** 
  281.      * Apply the specified alpha to the system navigation bar. 
  282.      * 
  283.      * @param alpha The alpha to use 
  284.      */  
  285.     @TargetApi(11)  
  286.     public void setNavigationBarAlpha(float alpha) {  
  287.         if (mNavBarAvailable && Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {  
  288.             mNavBarTintView.setAlpha(alpha);  
  289.         }  
  290.     }  
  291.   
  292.     /** 
  293.      * Get the system bar configuration. 
  294.      * 
  295.      * @return The system bar configuration for the current device configuration. 
  296.      */  
  297.     public SystemBarConfig getConfig() {  
  298.         return mConfig;  
  299.     }  
  300.   
  301.     /** 
  302.      * Is tinting enabled for the system status bar? 
  303.      * 
  304.      * @return True if enabled, False otherwise. 
  305.      */  
  306.     public boolean isStatusBarTintEnabled() {  
  307.         return mStatusBarTintEnabled;  
  308.     }  
  309.   
  310.     /** 
  311.      * Is tinting enabled for the system navigation bar? 
  312.      * 
  313.      * @return True if enabled, False otherwise. 
  314.      */  
  315.     public boolean isNavBarTintEnabled() {  
  316.         return mNavBarTintEnabled;  
  317.     }  
  318.   
  319.     private void setupStatusBarView(Context context, ViewGroup decorViewGroup) {  
  320.         mStatusBarTintView = new View(context);  
  321.         LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, mConfig.getStatusBarHeight());  
  322.         params.gravity = Gravity.TOP;  
  323.         if (mNavBarAvailable && !mConfig.isNavigationAtBottom()) {  
  324.             params.rightMargin = mConfig.getNavigationBarWidth();  
  325.         }  
  326.         mStatusBarTintView.setLayoutParams(params);  
  327.         mStatusBarTintView.setBackgroundColor(DEFAULT_TINT_COLOR);  
  328.         mStatusBarTintView.setVisibility(View.GONE);  
  329.         decorViewGroup.addView(mStatusBarTintView);  
  330.     }  
  331.   
  332.     private void setupNavBarView(Context context, ViewGroup decorViewGroup) {  
  333.         mNavBarTintView = new View(context);  
  334.         LayoutParams params;  
  335.         if (mConfig.isNavigationAtBottom()) {  
  336.             params = new LayoutParams(LayoutParams.MATCH_PARENT, mConfig.getNavigationBarHeight());  
  337.             params.gravity = Gravity.BOTTOM;  
  338.         } else {  
  339.             params = new LayoutParams(mConfig.getNavigationBarWidth(), LayoutParams.MATCH_PARENT);  
  340.             params.gravity = Gravity.RIGHT;  
  341.         }  
  342.         mNavBarTintView.setLayoutParams(params);  
  343.         mNavBarTintView.setBackgroundColor(DEFAULT_TINT_COLOR);  
  344.         mNavBarTintView.setVisibility(View.GONE);  
  345.         decorViewGroup.addView(mNavBarTintView);  
  346.     }  
  347.   
  348.     /** 
  349.      * Class which describes system bar sizing and other characteristics for the current 
  350.      * device configuration. 
  351.      * 
  352.      */  
  353.     public static class SystemBarConfig {  
  354.   
  355.         private static final String STATUS_BAR_HEIGHT_RES_NAME = "status_bar_height";  
  356.         private static final String NAV_BAR_HEIGHT_RES_NAME = "navigation_bar_height";  
  357.         private static final String NAV_BAR_HEIGHT_LANDSCAPE_RES_NAME = "navigation_bar_height_landscape";  
  358.         private static final String NAV_BAR_WIDTH_RES_NAME = "navigation_bar_width";  
  359.         private static final String SHOW_NAV_BAR_RES_NAME = "config_showNavigationBar";  
  360.   
  361.         private final boolean mTranslucentStatusBar;  
  362.         private final boolean mTranslucentNavBar;  
  363.         private final int mStatusBarHeight;  
  364.         private final int mActionBarHeight;  
  365.         private final boolean mHasNavigationBar;  
  366.         private final int mNavigationBarHeight;  
  367.         private final int mNavigationBarWidth;  
  368.         private final boolean mInPortrait;  
  369.         private final float mSmallestWidthDp;  
  370.   
  371.         private SystemBarConfig(Activity activity, boolean translucentStatusBar, boolean traslucentNavBar) {  
  372.             Resources res = activity.getResources();  
  373.             mInPortrait = (res.getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT);  
  374.             mSmallestWidthDp = getSmallestWidthDp(activity);  
  375.             mStatusBarHeight = getInternalDimensionSize(res, STATUS_BAR_HEIGHT_RES_NAME);  
  376.             mActionBarHeight = getActionBarHeight(activity);  
  377.             mNavigationBarHeight = getNavigationBarHeight(activity);  
  378.             mNavigationBarWidth = getNavigationBarWidth(activity);  
  379.             mHasNavigationBar = (mNavigationBarHeight > 0);  
  380.             mTranslucentStatusBar = translucentStatusBar;  
  381.             mTranslucentNavBar = traslucentNavBar;  
  382.         }  
  383.   
  384.         @TargetApi(14)  
  385.         private int getActionBarHeight(Context context) {  
  386.             int result = 0;  
  387.             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {  
  388.                 TypedValue tv = new TypedValue();  
  389.                 context.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true);  
  390.                 result = TypedValue.complexToDimensionPixelSize(tv.data, context.getResources().getDisplayMetrics());  
  391.             }  
  392.             return result;  
  393.         }  
  394.   
  395.         @TargetApi(14)  
  396.         private int getNavigationBarHeight(Context context) {  
  397.             Resources res = context.getResources();  
  398.             int result = 0;  
  399.             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {  
  400.                 if (hasNavBar(context)) {  
  401.                     String key;  
  402.                     if (mInPortrait) {  
  403.                         key = NAV_BAR_HEIGHT_RES_NAME;  
  404.                     } else {  
  405.                         key = NAV_BAR_HEIGHT_LANDSCAPE_RES_NAME;  
  406.                     }  
  407.                     return getInternalDimensionSize(res, key);  
  408.                 }  
  409.             }  
  410.             return result;  
  411.         }  
  412.   
  413.         @TargetApi(14)  
  414.         private int getNavigationBarWidth(Context context) {  
  415.             Resources res = context.getResources();  
  416.             int result = 0;  
  417.             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {  
  418.                 if (hasNavBar(context)) {  
  419.                     return getInternalDimensionSize(res, NAV_BAR_WIDTH_RES_NAME);  
  420.                 }  
  421.             }  
  422.             return result;  
  423.         }  
  424.   
  425.         @TargetApi(14)  
  426.         private boolean hasNavBar(Context context) {  
  427.             Resources res = context.getResources();  
  428.             int resourceId = res.getIdentifier(SHOW_NAV_BAR_RES_NAME, "bool""android");  
  429.             if (resourceId != 0) {  
  430.                 boolean hasNav = res.getBoolean(resourceId);  
  431.                 // check override flag (see static block)  
  432.                 if ("1".equals(sNavBarOverride)) {  
  433.                     hasNav = false;  
  434.                 } else if ("0".equals(sNavBarOverride)) {  
  435.                     hasNav = true;  
  436.                 }  
  437.                 return hasNav;  
  438.             } else { // fallback  
  439.                 return !ViewConfiguration.get(context).hasPermanentMenuKey();  
  440.             }  
  441.         }  
  442.   
  443.         private int getInternalDimensionSize(Resources res, String key) {  
  444.             int result = 0;  
  445.             int resourceId = res.getIdentifier(key, "dimen""android");  
  446.             if (resourceId > 0) {  
  447.                 result = res.getDimensionPixelSize(resourceId);  
  448.             }  
  449.             return result;  
  450.         }  
  451.   
  452.         @SuppressLint("NewApi")  
  453.         private float getSmallestWidthDp(Activity activity) {  
  454.             DisplayMetrics metrics = new DisplayMetrics();  
  455.             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {  
  456.                 activity.getWindowManager().getDefaultDisplay().getRealMetrics(metrics);  
  457.             } else {  
  458.                 // TODO this is not correct, but we don't really care pre-kitkat  
  459.                 activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);  
  460.             }  
  461.             float widthDp = metrics.widthPixels / metrics.density;  
  462.             float heightDp = metrics.heightPixels / metrics.density;  
  463.             return Math.min(widthDp, heightDp);  
  464.         }  
  465.   
  466.         /** 
  467.          * Should a navigation bar appear at the bottom of the screen in the current 
  468.          * device configuration? A navigation bar may appear on the right side of 
  469.          * the screen in certain configurations. 
  470.          * 
  471.          * @return True if navigation should appear at the bottom of the screen, False otherwise. 
  472.          */  
  473.         public boolean isNavigationAtBottom() {  
  474.             return (mSmallestWidthDp >= 600 || mInPortrait);  
  475.         }  
  476.   
  477.         /** 
  478.          * Get the height of the system status bar. 
  479.          * 
  480.          * @return The height of the status bar (in pixels). 
  481.          */  
  482.         public int getStatusBarHeight() {  
  483.             return mStatusBarHeight;  
  484.         }  
  485.   
  486.         /** 
  487.          * Get the height of the action bar. 
  488.          * 
  489.          * @return The height of the action bar (in pixels). 
  490.          */  
  491.         public int getActionBarHeight() {  
  492.             return mActionBarHeight;  
  493.         }  
  494.   
  495.         /** 
  496.          * Does this device have a system navigation bar? 
  497.          * 
  498.          * @return True if this device uses soft key navigation, False otherwise. 
  499.          */  
  500.         public boolean hasNavigtionBar() {  
  501.             return mHasNavigationBar;  
  502.         }  
  503.   
  504.         /** 
  505.          * Get the height of the system navigation bar. 
  506.          * 
  507.          * @return The height of the navigation bar (in pixels). If the device does not have 
  508.          * soft navigation keys, this will always return 0. 
  509.          */  
  510.         public int getNavigationBarHeight() {  
  511.             return mNavigationBarHeight;  
  512.         }  
  513.   
  514.         /** 
  515.          * Get the width of the system navigation bar when it is placed vertically on the screen. 
  516.          * 
  517.          * @return The width of the navigation bar (in pixels). If the device does not have 
  518.          * soft navigation keys, this will always return 0. 
  519.          */  
  520.         public int getNavigationBarWidth() {  
  521.             return mNavigationBarWidth;  
  522.         }  
  523.   
  524.         /** 
  525.          * Get the layout inset for any system UI that appears at the top of the screen. 
  526.          * 
  527.          * @param withActionBar True to include the height of the action bar, False otherwise. 
  528.          * @return The layout inset (in pixels). 
  529.          */  
  530.         public int getPixelInsetTop(boolean withActionBar) {  
  531.             return (mTranslucentStatusBar ? mStatusBarHeight : 0) + (withActionBar ? mActionBarHeight : 0);  
  532.         }  
  533.   
  534.         /** 
  535.          * Get the layout inset for any system UI that appears at the bottom of the screen. 
  536.          * 
  537.          * @return The layout inset (in pixels). 
  538.          */  
  539.         public int getPixelInsetBottom() {  
  540.             if (mTranslucentNavBar && isNavigationAtBottom()) {  
  541.                 return mNavigationBarHeight;  
  542.             } else {  
  543.                 return 0;  
  544.             }  
  545.         }  
  546.   
  547.         /** 
  548.          * Get the layout inset for any system UI that appears at the right of the screen. 
  549.          * 
  550.          * @return The layout inset (in pixels). 
  551.          */  
  552.         public int getPixelInsetRight() {  
  553.             if (mTranslucentNavBar && !isNavigationAtBottom()) {  
  554.                 return mNavigationBarWidth;  
  555.             } else {  
  556.                 return 0;  
  557.             }  
  558.         }  
  559.   
  560.     }  
  561.       
  562.     public void setStatusBarDarkMode(boolean darkmode, Activity activity) {  
  563.          if (sIsMiuiV6) {  
  564.              Class<? extends Window> clazz = activity.getWindow().getClass();  
  565.              try {  
  566.              int darkModeFlag = 0;  
  567.              Class<?> layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");  
  568.              Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");  
  569.              darkModeFlag = field.getInt(layoutParams);  
  570.              Method extraFlagField = clazz.getMethod("setExtraFlags"int.classint.class);  
  571.              extraFlagField.invoke(activity.getWindow(), darkmode ? darkModeFlag : 0, darkModeFlag);  
  572.              } catch (Exception e) {  
  573.                  e.printStackTrace();  
  574.              }  
  575.          }  
  576.     }  
  577. }  

用法:

  1. public void initSystemBar(){  
  2.         if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) {  
  3.             getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);  
  4.             getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);  
  5.             getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);  
  6.             fwRootLayout.setFitsSystemWindows(true);//需要把根布局设置为这个属性 子布局则不会占用状态栏位置  
  7.             fwRootLayout.setClipToPadding(true);//需要把根布局设置为这个属性 子布局则不会占用状态栏位置  
  8.         }  
  9.         tintManager = new SystemBarTintManager(this);// 创建状态栏的管理实例  
  10.         tintManager.setStatusBarTintEnabled(true);// 激活状态栏设置  
  11.         tintManager.setNavigationBarTintEnabled(true);// 激活导航栏设置  
  12.         tintManager.setStatusBarTintColor(getResources().getColor(R.color.blue500));//设置状态栏颜色  
  13.         tintManager.setStatusBarDarkMode(falsethis);//false 状态栏字体颜色是白色 true 颜色是黑色  
  14.     }  


截图





  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android 沉浸状态栏指的是在应用中隐藏系统状态栏,使应用的界面能够占据整个屏幕空间,提供更加沉浸的使用体验。在 Android 4.4 KitKat(API 级别 19)及以上版本中,引入了沉浸状态栏的支持。 要实现沉浸状态栏,可以按照以下步骤进行操作: 1. 在 AndroidManifest.xml 文件中,为对应的 Activity 设置 `android:theme` 属性为 `@style/Theme.AppCompat.NoActionBar` 或者其他无 ActionBar 特性的主题。 2. 在对应 Activity 的 `onCreate` 方法中添加以下代码来隐藏系统状态栏: ```java if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS); } ``` 3. 如果你想要在状态栏下方留出一定的空间,可以设置 paddingTop,例如: ```java if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { int statusBarHeight = getStatusBarHeight(); View view = findViewById(R.id.your_view_id); view.setPadding(0, statusBarHeight, 0, 0); } private int getStatusBarHeight() { int result = 0; int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android"); if (resourceId > 0) { result = getResources().getDimensionPixelSize(resourceId); } return result; } ``` 这样就可以实现 Android 沉浸状态栏的效果。需要注意的是,沉浸状态栏可能会导致一些 UI 布局的问题,需要根据具体情况进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值