如何在 android 中更改状态栏通知图标的 color/tint(棉花糖和 23+ 以上)?

How to change the status bar notification icons' color/tint in android (marshmallow and above 23+)?

如标题所述,如何将状态栏图标的颜色更改为深色而不是默认的白色。

来自

要使状态栏图标的颜色变暗而不是默认的白色,请在您的 styles.xml(或更准确地说是 values-v23/styles.xml)文件中添加以下标记:

<item name="android:windowLightStatusBar" tools:targetApi="23">true</item>

您还可以在运行时更改标志,方法是将其设置为任何 View:

View yourView = findViewById(R.id.your_view);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    if (yourView != null) {
        yourView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
    }
}

如果要重置更改,请像这样清除标志:

yourView.setSystemUiVisibility(0);

下面是示例代码,在纵向和横向之间切换时更改状态栏颜色。肖像模式:灯条,深色图标;横向模式:深色条,浅色图标;主题:"Theme.AppCompat.Light"

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        Window window = getWindow();
        View decorView = window.getDecorView();
        if(Configuration.ORIENTATION_LANDSCAPE == newConfig.orientation) {
            decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                window.setStatusBarColor(Color.parseColor("#55000000")); // set dark color, the icon will auto change light
            }
        } else {
            decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE|View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                window.setStatusBarColor(Color.parseColor("#fffafafa"));
            }
        }
    }