如何将状态栏变为白色并显示黑色图标?

107

我想改变我的应用程序状态栏的颜色,使其为白色背景黑色图标(而不是默认的黑色背景白色图标)。有什么方法可以实现这个功能吗?


更改您的默认主题。 - Rohit5k2
那么对于遵循Android指南并具有白色通知图标的应用程序,当用户在您的应用程序中时,您希望他们如何看到它们? - tyczj
是的,这不可能发生,你的应用程序没有控制通知栏,更别提其他应用程序资源了。 - tyczj
实际上,该应用程序可以控制棒棒糖的颜色 - 请参见https://dev59.com/x2Eh5IYBdhLWcg3wpEtF - Jon
2
但是您不能更改其他应用程序资源的颜色,事实上,Lollipop 会使所有图标变为白色,无论如何。 - tyczj
显示剩余6条评论
14个回答

148

使用Android M(API级别23),您可以通过主题的android:windowLightStatusBar属性实现此目的。

编辑:

正如Pdroid所提到的,这也可以通过编程实现:

getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR); 

2
我可以通过编程设置它吗? - busylee
@busylee 我猜你可以创建一个主题的子类,在那里进行更改,然后使用activity.getTheme.setTo(Theme another)应用。 - bugraoral
1
@busylee 是的,你可以随时使用以下方法进行设置:View.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR)。应该将其添加到答案中。 - Pdroid
据我所知,图标着色并不受支持,而且很有可能他们不会允许它以保持系统的一致性。@Dahnark - bugraoral
8
如果我想在22或之前的版本中使用,该怎么办? - Dhara Patel
显示剩余7条评论

64

如果SDK的版本>=23,可以通过以下方式使白色状态栏上的图标变为灰色(请参考文档):

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

在您的styles.xml中,将colorPrimary设置为白色或通过编程方式实现:

getWindow().setStatusBarColor(Color.WHITE);

16

刚刚添加到我的Kotlin活动中:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                window.decorView.systemUiVisibility =View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
                window.statusBarColor = Color.WHITE
            }

15

这是对我有效的方法

<style name="MyTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowLightStatusBar">true</item>
    <item name="android:statusBarColor">@android:color/white</item>
</style>

3
需要最低 API 级别为 23。 - Zurmati
@Zurmati 可能吧。我在我的应用程序中不支持 API 24 以下的任何内容。 - Raphael C

10

只需将此添加到您的样式中

<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">@android:color/transparent</item>

android:windowDrawsSystemBarBackgrounds设置为true*。 这是一个标志,其描述如下:

指示此窗口是否负责绘制系统栏的背景的标志。如果为true且窗口不浮动,则系统栏将以透明背景绘制,并且在此窗口中对应区域将填充在{@link android.R.attr#statusBarColor}和{@link android.R.attr#navigationBarColor}中指定的颜色。对应于{@link android.view.WindowManager.LayoutParams#FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS}。


3
 In your app theme use color for status bar like this 
 <style name="Theme.YourAppName" 
    parent="Theme.MaterialComponents.DayNight.NoActionBar">
    <!-- Primary brand color. -->
    <item name="colorPrimary">@color/statusBar</item>
    <item name="statusBarBackground">@color/statusBar</item>
    <item name="colorOnPrimary">@color/white</item>
    <!-- Secondary brand color. -->
    <item name="colorSecondary">@color/teal_200</item>
    <item name="colorSecondaryVariant">@color/teal_700</item>
    <item name="colorOnSecondary">@color/black</item>
    <!-- Status bar color. -->


    <item name="android:statusBarColor" tools:targetApi="l">? attr/statusBarBackground</item>

 use this code for icon change color
    <item name="android:windowLightStatusBar">true</item>
    <!-- Customize your theme here. -->
</style>

3

您可以在 Kotlin 中这样使用它

 window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR

在调用setContentView()之前,务必在onCreate()中调用它。

2

您可以使用最佳解决方案之一:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            val decor = this.window.decorView
            // valu reflected to set status bar icons in black
            if (value) {
                decor.systemUiVisibility = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
            } else {
                // We want to change tint color to white again.
                // You can also record the flags in advance so that you can turn UI back completely if
                // you have set other flags before, such as translucent or full screen.
                decor.systemUiVisibility = 0
            }
        }

这段代码的灵感来源于这条评论:https://dev59.com/YV0a5IYBdhLWcg3wqaNS#37694455


1

我刚刚使用了这个

  1. 在样式中设置父级元素
parent="Theme.AppCompat.Light.NoActionBar"
  1. Add this to the menu bar in xml
  app:theme="@style/AppThemeWithoutActionBarwithwhitehed"       
  android:background="#fff"

0

我使用以下方法实现:

  if (Build.VERSION.SDK_INT >= 19 && Build.VERSION.SDK_INT < 21) {
        setWindowFlag(this, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, true);
    }
    if (Build.VERSION.SDK_INT >= 19) {
        setWindowFlag(this, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, false);     }

    if (Build.VERSION.SDK_INT >= 21) {
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        getWindow().setStatusBarColor(Color.WHITE);
    }



setContentView(R.layout.activity_mainss);

主题在 AndroidManifest 中设置:

 <style name="Theme.MapRiti" parent="Theme.AppCompat.DayNight.DarkActionBar">
    <!-- Primary brand color. -->
    <item name="colorPrimary">@color/purple_500</item>
    <item name="colorPrimaryDark">@color/orange</item>
    <item name="colorPrimaryVariant">@color/purple_700</item>
    <item name="colorSecondary">@color/teal_200</item>
    <item name="colorSecondaryVariant">@color/teal_700</item>
</style>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接