以编程方式安装Android M Light and Dark状态栏 - 如何让它再次变黑?

时间:2016-06-07 07:16:06

标签: android user-interface android-6.0-marshmallow android-statusbar

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

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

或者我们使用此代码在运行时设置它:

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

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

我已经尝试过这些变种:

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

那么如何才能以正确的方式完成?

12 个答案:

答案 0 :(得分:38)

@Aracem发布的解决方案是有效的,但如果您尝试更改状态栏的背景颜色,则无效。就我而言,我是按照以下方式进行的。

启用windowLightStatusBar(以编程方式,在Utils类中):

 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恢复到以前的状态:

  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),才会发生恢复操作。

答案 1 :(得分:28)

根据Nick Butcher的项目&#34; Plaid&#34;

public static void clearLightStatusBar(@NonNull View view) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        int flags = view.getSystemUiVisibility();
        flags &= ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
        view.setSystemUiVisibility(flags);
    }
}

您可以找到项目Here

答案 2 :(得分:9)

我基于@Aracem和@CarlosHernándezGil,但是如果我们使用按位XOR (Java中的 ^ 运算符),我认为这很容易理解

private void setLightStatusBar(Activity activity) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        int flags = activity.getWindow().getDecorView().getSystemUiVisibility(); // get current flag
        flags |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;   // add LIGHT_STATUS_BAR to flag
        activity.getWindow().getDecorView().setSystemUiVisibility(flags); 
        activity.getWindow().setStatusBarColor(Color.GRAY); // optional
    }
}

private void clearLightStatusBar(Activity activity) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        int flags = activity.getWindow().getDecorView().getSystemUiVisibility(); // get current flag
        flags = flags ^ View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR; // use XOR here for remove LIGHT_STATUS_BAR from flags
        activity.getWindow().getDecorView().setSystemUiVisibility(flags);
        activity.getWindow().setStatusBarColor(Color.GREEN); // optional
    }
}

说明

首先,查看SYSTEM_UI_FLAG_LIGHT_STATUS_BARsetSystemUiVisibility

/**
 * Flag for {@link #setSystemUiVisibility(int)}: Requests the status bar to draw in a mode that
 * is compatible with light status bar backgrounds.
 */
public static final int SYSTEM_UI_FLAG_LIGHT_STATUS_BAR = 0x00002000;

public void setSystemUiVisibility(int visibility) {
    if (visibility != mSystemUiVisibility) {
        mSystemUiVisibility = visibility;
        ...
    }
}

我认为下面的2行代码很难理解

flags |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR; // for set light status bar
flags = flags ^ View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR; // for clear light status bar

乍看之下,我只是认为我们可以使用简单的赞

flags = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR; // for set light status bar
flags = 0; // for clear light status bar (0 <=> LIGHT_STATUS_BAR <=> default systemUiVisibility)

但是我们应该使用|^,因为
例如,我们要将状态栏和导航栏都设置为亮,那么我们将使用

flags = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR | View.View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
activity.getWindow().getDecorView().setSystemUiVisibility(flags);

当我们不希望状态栏变亮时,我们可以使用

flags = View.View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
activity.getWindow().getDecorView().setSystemUiVisibility(flags);

OR

flags = activity.getWindow().getDecorView().getSystemUiVisibility();
flags = flags ^ View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR; 
activity.getWindow().getDecorView().setSystemUiVisibility(flags);

要进一步了解为什么我们使用|^,我认为下面的教程可能会有所帮助 https://medium.com/@JakobUlbrich/flag-attributes-in-android-how-to-use-them-ac4ec8aee7d1 这是我的理解。希望有帮助

答案 3 :(得分:3)

我将这个简单的实用程序对象放在一起,使您可以在任何片段中更改状态栏的颜色和灯光状态栏的开/关。但是,这依赖于使用Android Jetpack导航组件进行导航(Kotlin):

object StatusBarUtil {
    fun changeStatusBarColor(activity: Activity, @ColorInt color: Int, lightStatusBar: Boolean) {
        activity.window?.let { win ->
            val nav = Navigation.findNavController(activity, R.id.your_nav_host_fragmen /* TODO: Use the ID of your nav host fragment */)
            val currentDest = nav.currentDestination?.id
            val oldColor = win.statusBarColor
            val oldFlags = win.decorView.systemUiVisibility
            win.statusBarColor = color

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                var flags = oldFlags
                flags = if (lightStatusBar) {
                    flags or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
                } else {
                    flags and View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR.inv()
                }
                win.decorView.systemUiVisibility = flags
            }

            nav.addOnNavigatedListener { _, dest ->
                if (dest.id != currentDest) {
                    win.statusBarColor = oldColor
                    win.decorView.systemUiVisibility = oldFlags
                }
            }
        }
    }
}

要使用此功能,请在任何片段的onViewCreated中调用以下命令:

StatusBarUtil.changeStatusBarColor(requireActivity(), someDarkColor, false)

答案 4 :(得分:2)

要更改为灯光状态栏,请使用:-

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) 
     activity?.window?.decorView?.systemUiVisibility = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR

要更改回黑暗状态栏:-

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) 
     activity?.window?.decorView?.systemUiVisibility = 0

答案 5 :(得分:2)

SDK的API 30稍有变化,现在,灯光状态栏的外观由WindowInsetsController控制,可以从Window获取。 以下是Kotlin中的一个示例方法(在Activity中),该方法将新API与以前使用的View.setSystemUiVisibility结合在一起,用于较旧的Android SDK版本。请记住,这仅会更改状态栏的系统图标外观,并且状态栏的实际颜色仍可以通过Window.setStatusBarColor进行设置。

@Suppress("DEPRECATION")
private fun setSystemUiLightStatusBar(isLightStatusBar: Boolean) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            val systemUiAppearance = if (isLightStatusBar) {
                WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS
            } else {
                0
            }
            window.insetsController?.setSystemBarsAppearance(systemUiAppearance,
                                                             WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS)
        } else {
            val systemUiVisibilityFlags = if (isLightStatusBar) {
                window.decorView.systemUiVisibility or SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
            } else {
                window.decorView.systemUiVisibility and SYSTEM_UI_FLAG_LIGHT_STATUS_BAR.inv()
            }
            window.decorView.systemUiVisibility = systemUiVisibilityFlags
        }
    }
}

答案 6 :(得分:1)

我会在上面的答案中做一些改变。

上课

 public class DarkStatusBar {
    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);
        }
    }
}

并在任何地方调用它

        Window window = getWindow();
        View view = window.getDecorView();
        DarkStatusBar.setLightStatusBar(view,this);

答案 7 :(得分:0)

基于@ phan-van-linh的答案,我为Xamarin Android编写了此类。

public static class ActivityExtensions
{
    public static void SetLightStatusBar(this Activity activity)
    {
        int flags = (int)activity.Window.DecorView.SystemUiVisibility; // get current flag
        flags |= (int)SystemUiFlags.LightStatusBar;   // add LIGHT_STATUS_BAR to flag
        activity.Window.DecorView.SystemUiVisibility = (StatusBarVisibility)flags;
        //activity.Window.SetStatusBarColor(Color.GRAY); // optional
    }

    public static void ClearLightStatusBar(this Activity activity)
    {
        int flags = (int)activity.Window.DecorView.SystemUiVisibility; // get current flag
        flags = flags ^ (int)SystemUiFlags.LightStatusBar; // use XOR here for remove LIGHT_STATUS_BAR from flags
        activity.Window.DecorView.SystemUiVisibility = (StatusBarVisibility)flags;
        //activity.Window.setStatusBarColor(Color.GREEN); // optional
    }
}

答案 8 :(得分:0)

设置具有浅色 kotlin 版本的蓝色背景状态栏

fun setBlueStatusBarColor(window: Window, context: Context) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            window.statusBarColor = context.getColor(R.color.colorBlue)
        }else {
            window.statusBarColor = context.resources.getColor(R.color.colorBlue)
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            var flags: Int = window.decorView.systemUiVisibility
            flags = flags and View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
            window.decorView.systemUiVisibility = flags
        }
    }
}

答案 9 :(得分:0)

/**
 * Changes color of the status bar icons
 * @param isLight if true - shows dark icons, light else
 */
fun setStatusBarUiTheme(activity: Activity?, isLight: Boolean) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        activity?.window?.decorView?.let {
            it.systemUiVisibility = if (isLight)
                it.systemUiVisibility or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR // dark icons
            else
                it.systemUiVisibility and View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR.inv() // light icons
        }
    }
}

答案 10 :(得分:0)

在 res/styles.xml 中

<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
    <item name="android:windowLightStatusBar">true</item>
    .......
</style>

<style name="AppTheme.DarkStatus" parent="AppTheme" tools:targetApi="23" >
    <item name="android:windowLightStatusBar">false</item>
    <item name="android:statusBarColor" >@color/status_bar_color</item>
</style>

代码中

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTheme(R.style.AppTheme_DarkStatus);  //To set DarkStatusBar theme
    setContentView(R.layout.activity_drawer);
    ....
}

答案 11 :(得分:0)

我为 API 23-30 切换明暗的方式与这些略有不同。这是一个kotlin版本

由于我使用 Compose 和 Crossfade 动画来更改主题,因此在某些情况下会调用此函数两次,从而使 xor 自行撤消。另一种方法是逆 or 操作。我的轻主题切换器最终看起来像这样

@Suppress("DEPRECATION")
fun invertInsets(darkTheme: Boolean, window: Window) {
    if (Build.VERSION.SDK_INT >= 30) {
        //Correct way of doing things
        val statusBar = APPEARANCE_LIGHT_STATUS_BARS
        val navBar = APPEARANCE_LIGHT_NAVIGATION_BARS
        if (!darkTheme) {
            window.insetsController?.setSystemBarsAppearance(statusBar, statusBar)
            window.insetsController?.setSystemBarsAppearance(navBar, navBar)
        } else {
            window.insetsController?.setSystemBarsAppearance(0, statusBar)
            window.insetsController?.setSystemBarsAppearance(0, navBar)
        }
    } else {
        // Does bitwise operations (or to add, inverse or to remove)
        // This is depreciated but the new version is API 30+ so I should have this here
        val flags = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR or
            if (Build.VERSION.SDK_INT >= 26) View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR else 0

        if (!darkTheme) {
            window.decorView.systemUiVisibility = 
                window.decorView.systemUiVisibility or flags
        } else {
            window.decorView.systemUiVisibility = 
                (window.decorView.systemUiVisibility.inv() or flags).inv()
        }
    }
}

API 30+ 的部分没有折旧,但实际上 API 30 的手机并不多,所以也有较低 API 的部分

为了简洁起见,它只是预先计算标志(因为设置 LIGHT_NAVIGATION_BARS 是 API 26+),然后明确地设置或重置那些确切的标志。没有 andxor 有趣的业务。 or 将始终将标志设置为 1,反之或事物将始终将标志设置为 0。然而,这是唯一可能的,因为 SYSTEM_UI_FLAG_LIGHT_STATUS_BARSYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR 都是一位。否则它可能需要使用 xor