【问题标题】:How can I make the status bar white with black icons?如何使状态栏变为白色并带有黑色图标?
【发布时间】:2015-02-21 18:55:06
【问题描述】:

我想更改我的应用程序状态栏的颜色,使其为带有黑色图标的白色(而不是默认的带有白色图标的黑色)。有没有办法做到这一点?

【问题讨论】:

  • 更改您的默认主题
  • 我应该把它改成什么主题才能将通知栏图标变成黑色?
  • 使用自定义主题。首先请参阅stackoverflow.com/questions/8024706/…
  • 那么那些遵循 android 指南并带有白色通知图标的应用程序呢,您希望用户在您的应用程序中看到它们时如何?
  • 但是你不能改变其他应用资源的颜色,事实上棒棒糖无论如何都会让所有图标变白

标签: android themes


【解决方案1】:

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

编辑:

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

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

【讨论】:

  • 我可以通过编程方式设置吗?
  • @busylee 我想你可以创建一个主题的子主题并在那里更改它并使用 activity.getTheme.setTo(Theme another)
  • 嗨@wrecker 我的应用程序通知图标也有同样的问题。我在我的主题中添加了 android:windowLightStatusBar=true 并且我正在 Android M 中对其进行测试。但是当我使用第三方启动器应用程序 (Nova Launcher) 更改主题时,白色通知图标不会变为灰色。我已经尝试了所有可能的方法,但还没有修复它。如果您能帮助我,将不胜感激。
  • @busylee 是的,您可以随时使用以下方法设置它: View.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);它应该添加到答案中
  • 如果我想在 22 或以前的版本中使用怎么办?
【解决方案2】:

可以使用灰色图标制作白色状态栏,例如这种方式适用于 SDK >= 23(参见 docs):

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

在您的styles.xml 中并将colorPrimary 设置为白色或以编程方式:

getWindow().setStatusBarColor(Color.WHITE);

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      【解决方案4】:

      这对我有用

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

      【讨论】:

      • 这需要最低 Api 级别 23
      • @Zurmati 可能。我的任何应用都不支持低于 api 24 的任何内容。
      【解决方案5】:

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

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

      【讨论】:

        【解决方案6】:

        你可以在 Kotlin 中这样使用它

         window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
        

        确保在setContentView()之前调用它在onCreate()

        【讨论】:

          【解决方案7】:

          除非您可以控制整个 rom 来手动自定义,否则没有这样的方法。我建议你做的是,通过你的主题使用浅灰色作为状态栏颜色,就像谷歌驱动器一样。

          编辑:请参考@Wrekcker 的答案,因为这在 android M 中发生了变化。

          【讨论】:

          • Android M 支持
          【解决方案8】:

          我刚用过这个

          1. 在样式中设置父级
          parent="Theme.AppCompat.Light.NoActionBar"
          
          1. 将此添加到 xml 中的菜单栏
            app:theme="@style/AppThemeWithoutActionBarwithwhitehed"       
            android:background="#fff"
          

          【讨论】:

            【解决方案9】:

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

            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://stackoverflow.com/a/37694455/10277217

            【讨论】:

              【解决方案10】:

              我使用以下方法实现了它:

                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>
              

              【讨论】:

                【解决方案11】:

                我遇到了同样的问题。

                我注意到当我使用像 RelativeLayout 这样的普通布局时,它不起作用。但是当我从像 CordinatorLayout 这样的支持库切换到它时,它终于开始工作了。试试这个。

                  <?xml version="1.0" encoding="utf-8"?>
                    <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
                        xmlns:app="http://schemas.android.com/apk/res-auto"
                        xmlns:tools="http://schemas.android.com/tools"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:fitsSystemWindows="true"
                        tools:context=".Activities.Activity">
                
                
                        <android.support.v7.widget.Toolbar
                            android:id="@+id/tb_activity"
                            android:layout_width="match_parent"
                            android:layout_height="?attr/actionBarSize"
                            android:background="?attr/colorPrimary"
                            app:popupTheme="@style/AppTheme.PopupOverlay" />
                
                
                    </android.support.design.widget.CoordinatorLayout>
                

                样式

                <resources>
                <!-- Base application theme. -->
                <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
                    <!-- Customize your theme here. -->
                    <item name="colorPrimary">@color/colorPrimary</item>
                    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
                    <item name="colorAccent">@color/colorAccent</item>
                </style>
                
                <style name="AppTheme.NoActionBar" parent="AppTheme">
                    <item name="windowActionBar">false</item>
                    <item name="windowNoTitle">true</item>
                </style>
                
                <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
                
                <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
                

                在清单中

                <activity
                            android:name=".Activities.MyActivity"
                            android:label="Activity"
                            android:theme="@style/AppTheme.NoActionBar">
                        </activity>
                

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 2019-02-24
                  • 1970-01-01
                  • 2017-08-16
                  • 1970-01-01
                  • 1970-01-01
                  • 2016-03-17
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多