如何在Android中更改状态栏图标颜色? [英] How to change status bar icon color in Android?

查看:192
本文介绍了如何在Android中更改状态栏图标颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将状态栏图标颜色从白色更改为黑色。我尝试下面的代码,但我无法做到。你可以帮我吗?

I want to change status bar icons color from white to black. I try below code but I can't make it. Could you help me please?

活动代码:

public static void setLightStatusBar(View view,Activity activity){
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

        int flags = view.getSystemUiVisibility();
        flags |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
        view.setSystemUiVisibility(flags);
        activity.getWindow().setStatusBarColor(Color.WHITE);
    }
}

style.xml代码:

style.xml code:

<item name="android:windowLightStatusBar">true</item>


推荐答案

试试这个。

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    activity.getWindow().getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN|View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
    // edited here 
    activity.getWindow().setStatusBarColor(Color.WHITE);
}

然后设置为根布局

android:fitsSystemWindows="true"

另一种方式

在您的代码中尝试此操作。

Try this in your code .

public static int StatusBarLightMode(Activity activity) {
    int result = 0;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        if (MIUISetStatusBarLightMode(activity, true)) {
            result = 1;
        } else if (FlymeSetStatusBarLightMode(activity.getWindow(), true)) {
            result = 2;
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
            result = 3;
        }
    }
    return result;
}


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;
}

public static boolean MIUISetStatusBarLightMode(Activity activity, boolean dark) {
    boolean result = false;
    Window window = activity.getWindow();
    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;

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (dark) {
                    activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
                } else {
                    activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return result;
}

然后使用 StatusBarLightMode(this);

它可以使您的状态文本和图标变为黑色。

And it can make your status text and icon to be black .

编辑

从Android 6.0开始,Google官方提供对配置的支持<样式属性$ b $ c中的code> android:windowLightStatusBar 是,当设置为true时,状态栏的背景颜色为浅色时,状态栏的文本颜色变为灰色false。

Starting with Android 6.0, Google official provides support for configuring android:windowLightStatusBar in the style property Yes, when set to true, when the background color of the statusbar is light, the text color of the statusbar becomes grayed out for false.

<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>

这篇关于如何在Android中更改状态栏图标颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆