【问题标题】:Status bar turns white and does not show content behind it状态栏变白,后面不显示内容
【发布时间】:2015-12-02 10:06:18
【问题描述】:

我正在 Marshmallow 上试用 AppCompat。我想要一个透明的状态栏,但它会变成白色。我尝试了几种解决方案,但它们对我不起作用(Transparent status bar not working with windowTranslucentNavigation="false"Lollipop : draw behind statusBar with its color set to transparent)。这是相关代码。

我的样式.xml

<style name="Bacon" parent="Theme.Bacon"/>

<style name="Theme.Bacon" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/theme_primary</item>
    <item name="colorPrimaryDark">@color/theme_primary_dark</item>
    <item name="colorAccent">@color/theme_accent</item>
    <item name="windowActionBar">false</item>
    <item name="windowActionBarOverlay">true</item>
    <item name="windowNoTitle">true</item>
    <item name="android:windowBackground">@color/background_material_light</item>  
</style>

<style name="Theme.Bacon.Detail" parent="Bacon"/>

v21

<style name="Bacon" parent="Theme.Bacon">
    <item name="android:windowDrawsSystemBarBackgrounds">true</item>
</style>

<style name="Theme.Bacon.Detail" parent="Bacon">
    <item name="android:statusBarColor">@android:color/transparent</item>
</style>

活动

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true" />

</FrameLayout>

片段

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="192dp"
    android:fitsSystemWindows="true"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:expandedTitleMarginBottom="32dp"
        app:expandedTitleMarginEnd="64dp"
        app:expandedTitleMarginStart="48dp"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        app:statusBarScrim="@color/black_trans80">

        <ImageView
            android:id="@+id/photo"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:contentDescription="@string/photo"
            android:fitsSystemWindows="true"
            android:scaleType="centerCrop"
            app:layout_collapseMode="parallax" />

        <android.support.v7.widget.Toolbar
            android:id="@+id/anim_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="pin"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
    </android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>

【问题讨论】:

  • 你能解决这个问题吗?我有同样的问题。在我的一项活动中,状态栏为白色
  • @kirtan403 我通过将我的 Activity 布局从 FrameLayout 更改为 RelativeLayout 来解决我的问题。请看我的回答。
  • 我的是相对的,只是仍然获得白色状态栏。我在这里发布了问题:stackoverflow.com/q/33890066/1820644
  • 经过一天的努力,我找到了适合我的解决方案。这是答案:stackoverflow.com/a/33892569/1820644
  • 我将 android:windowTranslucentStatus 状态更改为 false 并且可以正常工作

标签: android android-support-library android-appcompat androiddesignsupport android-6.0-marshmallow


【解决方案1】:

我在这个链接中找到了答案:Status Bar Color not changing with Relative Layout as root element

所以事实证明我们需要删除

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

在样式.xml(v21) 中。它对我来说很好用。

【讨论】:

  • 是的,这就是解决方案。因为 android:fitsSystemWindows 在使标签栏可滚动时会导致问题。找到了相同的答案,但您之前发布了它。还要提到我认为这是默认覆盖的。
  • 这应该被标记为接受的解决方案。这个问题在我的应用程序中蔓延,这个解决方案只是通过注释掉那一行来解决它。救了我这么头痛!
  • 我正在使用 CoordinatorLayout,因此对我们 CoordinatorLayout 作为根的另一个答案对我不起作用(按照建议使用 fitSystemWindows=true 也不起作用)。这个答案对我有用。
  • 如果我要求状态栏颜色透明怎么办?
  • @nullmn 那么您需要将您的活动背景更改为其他内容。
【解决方案2】:

(聚会有点晚了,但它可能会对某人有所帮助)

我遇到了完全相同的问题。不知何故,一些活动是正常的,而我创建的新活动显示白色状态栏而不是 colorPrimaryDark 值。

在尝试了几个技巧后,我注意到正常工作的活动都使用CoordinatorLayout 作为布局的根,而对于其他活动,我已将其替换为常规布局,因为我不需要这些功能(动画等)由CoordinatorLayout提供。

因此解决方案是将CoordinatorLayout 设为根布局,然后在其中添加您以前的布局根。这是一个例子:

<?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"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:fitsSystemWindows="true">

  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <!-- your activity content here-->


  </LinearLayout>
</android.support.design.widget.CoordinatorLayout>

请注意,如果没有 android:fitsSystemWindows="true",此解决方案对我不起作用。

在棒棒糖和棉花糖上测试

【讨论】:

  • 该解决方案也对我有用。但是..为什么会这样?当 root 为 DrawerLayout 或 CoordinatorLayout 时显示正确的 StatusBar 颜色
  • 这仅适用于我在 Android 8 中将 android:fitsSystemWindows="true" 添加到 CoordinatorLayout 时。
  • @Franco 我认为这是解决方案中最相关的部分。我这样说是因为当我遇到同样的问题时我有CoordinatorLayout,而为我解决这个问题的特定属性是android:fitsSystemWindows="true"
  • android:fitsSystemWindows="true" 对我不起作用。解决我的问题的方法是在带有CoordinatorLayout 的活动中使用不是AppTheme.NoActionBar 的主题。我不知道为什么会修复它。
  • CoordinatorLayoutandroid:fitsSystemWindows="true" 都不适合我。
【解决方案3】:

您必须在样式中添加此属性才能查看内容

<item name="android:windowLightStatusBar" tools:ignore="NewApi">true</item>

【讨论】:

  • 这对我有用。这应该是被接受的答案。
【解决方案4】:
 <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
        <item name="android:statusBarColor">@color/colorPrimaryDark</item>
    </style

只需替换这个,statusBarColor 应该是您预期的颜色,而不是 TRANSPARENT

【讨论】:

    【解决方案5】:

    在你的 style.xml 中添加这个

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

    这在你的 onCreate();

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

    【讨论】:

    • 谢谢,但这不起作用。状态栏看起来一样。
    • 请帮助解决这个问题...同样的问题在这里....到目前为止,没有任何解决方案有效
    • @SaeedJassani 我找到了一个适合我的解决方案。如果您有同样的问题,请查看我的问题和答案:stackoverflow.com/a/33892569/1820644
    【解决方案6】:

    只需将此项目放入您的 v21\styles.xml 中即可:

    是的

    应该是这样的:

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
        <item name="android:windowTranslucentStatus">true</item>
    </style>
    

    【讨论】:

    • 在最后两项中使用 false 值对我有用。很奇怪,但谢谢。
    • 它只适用于最后两行,但内容在状态栏后面,填充/边距或 fitSystemWindows 无济于事。在运行牛轧糖的 OP 5 上测试
    【解决方案7】:

    在尝试上述所有方法均未成功后,我发现明确设置主题可以解决问题。

    setTheme(R.style.AppTheme);
    

    这必须在你的活动中的 super.OnCreate() 之前。

    【讨论】:

    • 我可以确认这修复了 Marshmallow 上的错误活动
    • 爱你,工作就像一个魅力,虽然我仍然不知道为什么:@
    • 在 super.OnCreate() 之前是关键
    【解决方案8】:

    我遇到了同样的问题。我所做的是,在“v21/styles.xml”文件中更改了值 true:

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

    到:

     <item name="android:windowDrawsSystemBarBackgrounds">false</item>
    

    【讨论】:

      【解决方案9】:
      <item name="android:statusBarColor">@android:color/transparent</item>
      

      您将在 values/styles/styles.xml(v21) 中看到该行代码。删除它并解决问题

      Link to this answer

      【讨论】:

        【解决方案10】:

        我通过将我的 Activity 布局从 FrameLayout 更改为 RelativeLayout 来解决我的问题。感谢所有试图提供帮助的人!

        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:background="@android:color/transparent">
        
            <android.support.v4.view.ViewPager
              android:id="@+id/pager"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:background="@color/theme_primary_dark"
              android:fitsSystemWindows="true" />
        
        </RelativeLayout>
        

        试试hierarchy-viewerViewInspector。这些工具可能会对您有所帮助。

        【讨论】:

          【解决方案11】:

          只需从样式 v21 中删除以下标记 @android:颜色/透明

          这对我有用。

          【讨论】:

          • @android:color/transparent 不是“标签”。它可能是样式属性的值,但不是“标签”。如果 OP 不止一次定义了这个值怎么办?他应该删除哪一个?你的回答太笼统了。
          • 是的,我也是这样。谢谢你,Shendre! :) 对于您的评论,@protectedmember - 操作系统没有性别。
          【解决方案12】:

          我已经找到了解决方案 -

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

          将此添加到您的样式中,它将适用于 api 21 或更高版本。

          【讨论】:

            【解决方案13】:

            更好的方法

            检查您的 NoActionBar 主题所在的 style.xml 文件。 确保它的父级为 Theme.AppCompat.NoActionBar 并通过添加您的配色方案对其进行自定义。最后根据需要在清单中设置您的主题。

            以下是我的 styles.xml 文件的示例(ActionBar + NoActionBar)。

            <!-- 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>
            
            <!-- No ActionBar application theme. -->
            <style name="AppTheme.NoActionBar" parent="Theme.AppCompat.NoActionBar">
                <item name="colorPrimary">@color/colorPrimary</item>
                <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
                <item name="colorAccent">@color/colorAccent</item>
                <item name="windowActionBar">false</item>
                <item name="windowNoTitle">true</item>
            </style>
            

            【讨论】:

              【解决方案14】:

              对于样式 v23

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

              对于样式 v21

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

              【讨论】:

                【解决方案15】:

                对我来说,它通过以下方式起作用:

                1. 设置主题.NoActionBar
                2. 将工具栏包裹在android.support.design.widget.AppBarLayout
                3. android.support.design.widget.CoordinatorLayout 设为父布局。

                本质上是在colorPrimaryDark 中绘制状态栏的第三步,否则如果您使用NoActionBar 主题,则不会绘制。 第二步将为您的工具栏提供 overlay

                【讨论】:

                  【解决方案16】:

                  [

                  从项目面板展开 res -> values -> 样式目录。

                  打开styles.xml (v21)

                  使用以下方式中的任何一种

                  1. android:windowDrawsSystemBarBackgrounds 属性中将 true 更改为 false
                  2. android:statusBarColor 属性中将 @android:color/transparent 更改为 @color/colorPrimaryDark
                  3. 删除android:statusBarColor 属性行。

                  【讨论】:

                    【解决方案17】:

                    如果您的 v21/styles.xml 包含

                    <resources>
                        <style name="AppTheme.NoActionBar">
                            <item name="windowActionBar">false</item>
                            <item name="windowNoTitle">true</item>
                            <item name="android:statusBarColor">@android:color/transparent</item>
                        </style>
                    </resources>
                    

                    那么, 删除评论行下或更改状态栏的颜色,

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

                    它工作正常。希望这会有所帮助。谢谢。

                    【讨论】:

                      【解决方案18】:

                      我不确定是否晚了,但我希望它对某人有所帮助。 * 制作一个适合布局的具有透明背景的假视图,并将 coordinatorlayout 作为您的根布局元素。

                      <View
                          android:layout_width="match_parent"
                          android:layout_height="match_parent"
                          android:background="@android:color/transparent"
                          android:fitsSystemWindows="true" />
                      
                      
                      <ImageView
                          android:layout_width="match_parent"
                          android:layout_height="match_parent"
                          android:fitsSystemWindows="true"
                          android:scaleType="centerCrop"
                          android:src="@drawable/something" />
                      
                      <FrameLayout
                          android:layout_width="match_parent"
                          android:layout_height="match_parent">
                      
                         .... YOUR LAYOUT
                      

                      【讨论】:

                        【解决方案19】:

                        你想要这个吗?

                        试试这个。在 [值] - [style.xml]

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

                        你知道,不要匹配 apptheme - parent

                        祝你好运

                        【讨论】:

                          【解决方案20】:

                          我的猜测是这是由您的android:windowBackground 引起的。 @color/background_material_light 是指白色吗?

                          【讨论】:

                          • 不。它绝对不是白色的,但无论如何谢谢! (你可以看到上面的白色图标。)
                          • 我看错了你的问题。尝试将&lt;item name="android:statusBarColor"&gt;@android:color/transparent&lt;/item&gt; 替换为&lt;item name="android:statusBarColor"&gt;@null&lt;/item&gt;
                          • 只是为了确保我对您的理解正确,您希望图像显示在状态栏后面吗?
                          【解决方案21】:

                          检查这个 工具栏变白 - AppBar 滚出屏幕后未绘制

                          https://code.google.com/p/android/issues/detail?id=178037#c19

                          我正在使用库 23.0.0。并且错误仍然存​​在。

                          解决方法是添加几乎不占用空间且不可见的视图。请参阅下面的结构。

                          <android.support.v4.widget.NestedScrollView
                              app:layout_behavior="@string/appbar_scrolling_view_behavior">
                          
                          </android.support.v4.widget.NestedScrollView>
                          
                          <android.support.design.widget.AppBarLayout>
                          
                              <android.support.v7.widget.Toolbar
                                  app:layout_scrollFlags="scroll|enterAlways" />
                          
                              <android.support.design.widget.TabLayout
                                  app:layout_scrollFlags="scroll|enterAlways" />
                          
                              <View
                                  android:layout_width="match_parent"
                                  android:layout_height=".3dp"
                                  android:visibility="visible"/>
                          
                          </android.support.design.widget.AppBarLayout>
                          

                          Stackoverflow.com 上的这个问题也提到了这个错误: CoordinatorLayout Toolbar invisible on enter until full height AppBarLayout - Layout sometimes invisible once it enters view (even if it's not entered)

                          【讨论】:

                          • 这不是同一个错误。在我的情况下,它在活动显示后不是在滚动屏幕后显示为白色。还是谢谢
                          【解决方案22】:

                          如果您使用的是 RelativeLayout / CoordinatorLayout,这是适合我的解决方案:

                          你必须使用

                          • 协调器布局
                          • AppBarLayout

                          请记住使用 CoordinatorLayout 而不是 RelativeLayout(性能更好,与 AppBarLayout 完美配合)

                          这就是你的片段应该如何开始

                          <?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"
                          android:layout_width="match_parent"
                          android:layout_height="match_parent"
                          android:background="@android:color/background_light"
                          android:fitsSystemWindows="true"
                          >
                          
                          <android.support.design.widget.AppBarLayout
                              android:id="@+id/main.appbar"
                              android:layout_width="match_parent"
                              android:layout_height="300dp"
                              android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                              android:fitsSystemWindows="true"
                              >
                              ...
                          

                          编码不错!

                          【讨论】:

                            【解决方案23】:
                             <style name="AppTheme.NoActionBar">
                                <item name="windowActionBar">false</item>
                                <item name="windowNoTitle">true</item>
                                <item name="android:windowTranslucentStatus">true</item>
                                <item name="android:statusBarColor">@android:color/transparent</item>
                            </style>
                            

                            在样式中添加“windowTranslucentStatus”项

                            【讨论】:

                              【解决方案24】:

                              在你的 style.xml 中添加这个

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

                              注意:API 级别 21 以下不支持状态栏着色。

                              【讨论】:

                                【解决方案25】:

                                onCreate 内添加以下内容:

                                getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
                                       getWindow().setStatusBarColor(Color.WHITE);
                                

                                及以下

                                 setContentView(R.layout.activity_main);
                                

                                【讨论】:

                                  【解决方案26】:
                                      Window window = this.getWindow();
                                      window.setStatusBarColor(this.getResources().getColor(R.color.colorPrimaryDark));
                                  

                                  在下面的java文件中添加这2行

                                  【讨论】:

                                  • 欢迎来到 Stack Overflow。您能否详细说明您的代码是如何工作的以及它如何帮助解决 OPs 问题?请参考how to write a good answer 改进您的答案
                                  猜你喜欢
                                  • 2018-01-13
                                  • 1970-01-01
                                  • 1970-01-01
                                  • 2023-03-30
                                  • 1970-01-01
                                  • 1970-01-01
                                  • 1970-01-01
                                  • 1970-01-01
                                  • 2018-02-25
                                  相关资源
                                  最近更新 更多