1. 程式人生 > >android狀態列黑色字型,時間電池深色

android狀態列黑色字型,時間電池深色

首先感謝前人的努力探索:

http://www.jianshu.com/p/7f5a9969be53
http://blog.csdn.net/kezhenlu/article/details/51556160
http://blog.csdn.net/angcyo/article/details/49834739
http://www.open-open.com/lib/view/open1452308426605.html

4.4以上可以做狀態列的沉浸式,在沉浸式的顏色和時間wifi等顏色相同時,為了滿足設計,就要改變wifi等狀態列字型的顏色,首先來說,這是一個系統級別的功能,並不是什麼系統,什麼型號的手機都支援..適配淺色狀態列深色字型的時候發現底層版本為Android6.0.1的MIUI7.1系統不支援View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR設定,還是得用MIUI自己的深色字型方法。
目前可以是MIUI6+,Flyme4+,Android6.0+支援切換狀態列的文字顏色為暗色。

小米手機

public class MIUIHelper implements IHelper {

    /**
     * 設定狀態列字型圖示為深色,需要MIUI6以上
     *
     * @param isFontColorDark 是否把狀態列字型及圖示顏色設定為深色
     * @return boolean 成功執行返回true
     */
    @Override
    public boolean setStatusBarLightMode(Activity activity, boolean isFontColorDark) {
        Window window = activity.getWindow();
        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 (isFontColorDark) {
                    extraFlagField.invoke(window, darkModeFlag, darkModeFlag);//狀態列透明且黑色字型
                } else {
                    extraFlagField.invoke(window, 0, darkModeFlag);//清除黑色字型
                }
                result = true;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return result;
    }
}

魅族手機4+:
public class FlymeHelper implements IHelper {

    /**
     * 設定狀態列圖示為深色和魅族特定的文字風格
     * 可以用來判斷是否為Flyme使用者
     *
     * @param isFontColorDark 是否把狀態列字型及圖示顏色設定為深色
     * @return boolean 成功執行返回true
     */
    @Override
    public boolean setStatusBarLightMode(Activity activity, boolean isFontColorDark) {
        Window window = activity.getWindow();
        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 (isFontColorDark) {
                    value |= bit;
                } else {
                    value &= ~bit;
                }
                meizuFlags.setInt(lp, value);
                window.setAttributes(lp);
                result = true;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return result;
    }
}

android6.0+有兩種方法:

1,程式碼

public class AndroidMHelper implements IHelper {
    /**
     * @return if version is lager than M
     */
    @Override
    public boolean setStatusBarLightMode(Activity activity, boolean isFontColorDark) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (isFontColorDark) {
                // 沉浸式
                //                activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
                //非沉浸式
                activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
            } else {
                //非沉浸式
                activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
            }
            return true;
        }
        return false;
    }

}

2,xml形式
<!--直接生效,狀態列文字顏色變成黑色,非沉浸式-->
<item name="android:windowLightStatusBar">true</item>
統一方案:
<style name="statusBarStyle" parent="@android:style/Theme.DeviceDefault.Light">
    <item name="android:statusBarColor">@color/status_bar_color</item>
    <item name="android:windowLightStatusBar">false</item>
</style>

統一上面的方案,就有一下的適配程式碼:

暫時不貼了,其實都差不多,做 if --else --- 就行了.

OK.