白底黑字!Android淺色狀態欄黑色字體模式(另)


 

96 
小彬什么都想做任重致遠 
2016.06.30 10:16* 字數 489 閱讀 3234評論 3

前言

由於該死不死的設計濕,設計了一套白色狀態欄的UI。當然在iOS上可以實現自適應,但是安卓上比較麻煩,所以一直沒有搞。最近看到一篇文章《白底黑字!Android淺色狀態欄黑色字體模式》里面提及到解決辦法,才豁然開朗。

MIUI 8.0截圖


 
Screenshot_2016-06-30-09-39-51-101_com.linkit.lan.png

懶得打字,引用原作者的話

在Android6.0才開始支持SYSTEM_UI_FLAG_LIGHT_STATUS_BAR可以將狀態欄圖標改為灰色。

而Android4.4以上系統版本可以修改狀態欄顏色,但是只有小米的MIUI、魅族的Flyme和Android6.0以上系統可以把狀態欄文字和圖標換成深色,其他的系統狀態欄文字都是白色的,換成淺色背景的話就看不到。

適配淺色狀態欄深色字體的時候發現底層版本為Android6.0的MIUI系統不支持SYSTEM_UI_FLAG_LIGHT_STATUS_BAR設置,還是得用MIUI自己的深色字體方法。三方Android系統對底層改的挺亂的,也是讓人頭痛。O__O

導入1(如需要支持Android4.4的狀態欄變色,不需要則跳過這步)

在 build.gradle添加依賴:

dependencies {
    compile 'com.readystatesoftware.systembartint:systembartint:1.0.3' } 

或者直接導入這個類:
https://github.com/jgilfelt/SystemBarTint/blob/master/library/src/com/readystatesoftware/systembartint/SystemBarTintManager.java

導入2(如不需要支持Android4.4,把SystemBarTintManager部分去掉)

這里說一下,miui的K,L,M三個Android版本我都測試過了,但是由於沒有魅族的機子所以還沒測試過,也沒有找到EMUI的實現方法。這里引用了原作者的方法,並整合了一下。

    //白色可以替換成其他淺色系 @TargetApi(Build.VERSION_CODES.LOLLIPOP) public static void myStatusBar(Activity activity) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { if (MIUISetStatusBarLightMode(activity.getWindow(), true)) {//MIUI if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//5.0 activity.getWindow().setStatusBarColor(Color.WHITE); } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {//4.4 activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); SystemBarTintManager tintManager = new SystemBarTintManager(activity); tintManager.setStatusBarTintEnabled(true); tintManager.setStatusBarTintResource(android.R.color.white); } } else if (FlymeSetStatusBarLightMode(activity.getWindow(), true)) {//Flyme if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//5.0 activity.getWindow().setStatusBarColor(Color.WHITE); } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {//4.4 activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); SystemBarTintManager tintManager = new SystemBarTintManager(activity); tintManager.setStatusBarTintEnabled(true); tintManager.setStatusBarTintResource(android.R.color.white); } } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {//6.0 activity.getWindow().setStatusBarColor(Color.WHITE); activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR); } } } 
    /** * 設置狀態欄字體圖標為深色,需要MIUIV6以上 * * @param window 需要設置的窗口 * @param dark 是否把狀態欄字體及圖標顏色設置為深色 * @return boolean 成功執行返回true */ public static boolean MIUISetStatusBarLightMode(Window window, boolean dark) { boolean result = false; 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);//狀態欄透明且黑色字體 } else { extraFlagField.invoke(window, 0, darkModeFlag);//清除黑色字體 } result = true; } catch (Exception e) { } } return result; } 
    /** * 設置狀態欄圖標為深色和魅族特定的文字風格 * 可以用來判斷是否為Flyme用戶 * * @param window 需要設置的窗口 * @param dark 是否把狀態欄字體及圖標顏色設置為深色 * @return boolean 成功執行返回true */ 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; } 

參考文章

原文1:白底黑字!Android淺色狀態欄黑色字體模式
原文2:Android 6.0狀態欄使用灰色文字和圖標

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM