Android M的明暗状态栏通过编程方式-如何再次使其变暗? [英] Android M Light and Dark status bar programmatically - how to make it dark again?

查看:219
本文介绍了Android M的明暗状态栏通过编程方式-如何再次使其变暗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Android M中,我们可以使状态栏图标变暗。为此,我们可以在主题的xml中指定属性:

In the Android M we have ability to make status bar icons dark. To do that we can specify attribute in the theme's xml:

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

或者我们在运行时使用以下代码对其进行设置:

OR we cat set it at runtime with this code:

View someView = findViewById(R.id.some_view);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    someView.setSystemUiVisibility(someView.getSystemUiVisibility() | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
}

它实际上工作正常。但是问题是如何在运行时将状态栏模式正确设置为黑暗?

And it actually works fine. But question is how to properly set a status bar mode to dark at runtime?

我已经尝试了以下变体:

I already tried these variants:

// Makes status bar mode dark, but also hides it along with all navigation views. 
someView.setSystemUiVisibility(someView.getSystemUiVisibility() | ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);

// Does nothing 
someView.setSystemUiVisibility(someView.getSystemUiVisibility() & ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);

// Also does nothing 
someView.setSystemUiVisibility(someView.getSystemUiVisibility() ^ View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);

那么如何才能正确地做到这一点?

So how it can be done the right way?

推荐答案

@Aracem发布的解决方案是有效的,但是,如果您还尝试更改状态栏的背景色,则该解决方案将无效。就我而言,我是通过以下方式完成的。

The solution posted by @Aracem is valid but, doesn't work if you try change also the background color of the status bar. In my case I do it in the following way.

要启用windowLightStatusBar(以编程方式,例如在Utils类内部):

To enable windowLightStatusBar(programatically,inside a Utils class for example):

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

要将StatusBar还原到以前的状态,

To restore to StatusBar to the previous state:

  public static void clearLightStatusBar(Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            Window window = activity.getWindow();
            window.setStatusBarColor(ContextCompat
                 .getColor(activity,R.color.colorPrimaryDark)); 
        }
    }

恢复状态栏的颜色就足够了还恢复图标颜色。
非常重要:在setLightStatusBar(View view ..)中使用的视图从屏幕上消失(即view.getVisibility()== GONE | INVISIBLE)之前,将不会执行还原操作。

Restoring the color of the status bar is enough, it restores also the icons colors. VERY IMPORTANT: The restore operation will not occur until the view used in setLightStatusBar(View view..) dissapears(that is, view.getVisibility()==GONE|INVISIBLE) from the screen.

这篇关于Android M的明暗状态栏通过编程方式-如何再次使其变暗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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