1. 程式人生 > >android 沉浸式之改變小米狀態列顏色

android 沉浸式之改變小米狀態列顏色

這個是基於SystemBarTintManager更改的

增加一個方法:用於更改MIUIV6系統上的狀態列字型顏色 ,目前我僅僅只發現MIUIV6上可以更改,在android5.0上以及其它4.4以上系統沒有發現可以更改字型顏色的程式碼

核心程式碼:

[java]view plaincopyprint?
  1. publicvoid 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.     }  

全部程式碼: [java]view plaincopyprint?
  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. import java.lang.reflect.Field;  
  35. import java.lang.reflect.Method;  
  36. /** 
  37.  * Class to manage status and navigation bar tint effects when using KitKat  
  38.  * translucent system UI modes. 
  39.  * 
  40.  */
  41. publicclass SystemBarTintManager {  
  42.     /** 
  43.      * The default system bar tint color value. 
  44.      */
  45.     publicstaticfinalint DEFAULT_TINT_COLOR = 0x99000000;  
  46.     privatestatic String sNavBarOverride;  
  47.     privatefinal SystemBarConfig mConfig;  
  48.     privateboolean mStatusBarAvailable;  
  49.     privateboolean mNavBarAvailable;  
  50.     privateboolean mStatusBarTintEnabled;  
  51.     privateboolean mNavBarTintEnabled;  
  52.     private View mStatusBarTintView;  
  53.     private View mNavBarTintView;  
  54.     privatestaticboolean sIsMiuiV6;  
  55.     static {  
  56.         // Android allows a system property to override the presence of the navigation bar.
  57.         // Used by the emulator.
  58.         // See https://github.com/android/platform_frameworks_base/blob/master/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java#L1076
  59.         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {  
  60.             try {  
  61.                 Class c = Class.forName("android.os.SystemProperties");  
  62.                 Method m = c.getDeclaredMethod("get", String.class);  
  63.                 m.setAccessible(true);  
  64.                 sIsMiuiV6 = "V6".equals((String) m.invoke(c, "ro.miui.ui.version.name"));  
  65.                 sNavBarOverride = (String) m.invoke(null"qemu.hw.mainkeys");  
  66.             } catch (Throwable e) {  
  67.                 sNavBarOverride = null;  
  68.             }  
  69.         }  
  70.     }  
  71.     /** 
  72.      * Constructor. Call this in the host activity onCreate method after its 
  73.      * content view has been set. You should always create new instances when 
  74.      * the host activity is recreated. 
  75.      * 
  76.      * @param activity The host activity. 
  77.      */
  78.     @TargetApi(19)  
  79.     public SystemBarTintManager(Activity activity) {  
  80.         Window win = activity.getWindow();  
  81.         ViewGroup decorViewGroup = (ViewGroup) win.getDecorView();  
  82.         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {  
  83.             // check theme attrs
  84.             int[] attrs = {android.R.attr.windowTranslucentStatus,  
  85.                     android.R.attr.windowTranslucentNavigation};  
  86.             TypedArray a = activity.obtainStyledAttributes(attrs);  
  87.             try {  
  88.                 mStatusBarAvailable = a.getBoolean(0false);  
  89.                 mNavBarAvailable = a.getBoolean(1false);  
  90.             } finally {  
  91.                 a.recycle();  
  92.             }  
  93.             // check window flags
  94.             WindowManager.LayoutParams winParams = win.getAttributes();  
  95.             int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;  
  96.             if ((winParams.flags & bits) != 0) {  
  97.                 mStatusBarAvailable = true;  
  98.             }  
  99.             bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION;  
  100.             if ((winParams.flags & bits) != 0) {  
  101.                 mNavBarAvailable = true;  
  102.             }  
  103.         }  
  104.         mConfig = new SystemBarConfig(activity, mStatusBarAvailable, mNavBarAvailable);  
  105.