状态栏变白且不显示其后的内容

101
我正在尝试在Marshmallow上使用AppCompat。我想要一个透明状态栏,但它变成了白色。我尝试了几种解决方案,但它们对我没有起作用。 (Transparent status bar not working with windowTranslucentNavigation="false", Lollipop : draw behind statusBar with its color set to transparent). 这是相关的代码。
我的styles.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
1
@kirtan403 我通过将我的Activity布局从FrameLayout更改为RelativeLayout来解决了我的问题。请查看我的答案。 - pt2121
我的代码只是相对的,但仍然出现了白色状态栏。我在这里发布了问题:https://dev59.com/v5Hea4cB1Zd3GeqPomZm - kirtan403
在苦苦挣扎了一天之后,我终于找到了适合我的解决方案。以下是答案链接:https://dev59.com/v5Hea4cB1Zd3GeqPomZm#33892569 - kirtan403
我将android:windowTranslucentStatus状态更改为false,现在它可以正常工作了。 - Kiran Benny Joseph
我曾经遇到过同样的问题,并通过这个链接解决了它。 - Kishan Solanki
27个回答

171

是的,这就是解决方案。因为android:fitsSystemWindows会在制作选项卡栏可滚动时出现问题。我发现你之前也给出了相同的答案。同时提到这是默认被覆盖的。 - jobbert
这应该被标记为已接受的解决方案。这个问题在我的应用程序中到处都出现了,通过注释掉那一行代码,这个解决方案就可以在所有地方修复它。真是帮我省了不少麻烦! - BMB
我正在使用CoordinatorLayout,因此对于我们来说,将CoordinatorLayout用作根视图的其他答案对我没有用(使用建议的fitsSystemWindows=true也不起作用)。这个答案适用于我。 - Bruce
如果我需要我的状态栏颜色是透明的呢? - nullmn
1
@nullmn 那么你需要将你的Activity背景更改为其他内容。 - Phoenix Wang

99

有点晚了,但可能有助于某些人。

我曾经遇到过完全相同的问题。一些活动表现正常,而我创建的新活动则会显示白色状态栏,而不是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",此解决方案对我无效。

已在Lollipop和Marshmallow上测试。


3
这个解决方案对我也有效。但是为什么会出现这种情况呢?只有当根布局是DrawerLayout或CoordinatorLayout时,才会显示正确的StatusBar颜色。 - user3316561
3
只有在将 android:fitsSystemWindows="true" 添加到 Android 8 中的 CoordinatorLayout 上时,这个方法才对我有效。 - Franco
2
@Franco 我认为这是解决方案中最相关的部分。我这么说是因为当我遇到同样的问题时,我使用了 CoordinatorLayout,而特定的属性 android:fitsSystemWindows="true" 解决了我的问题。 - Wilhelm
android:fitsSystemWindows="true" 对我没有起作用。解决我的问题的方法是在带有 CoordinatorLayout 的活动中使用不是 AppTheme.NoActionBar 的主题。我不知道为什么会这样解决。 - bmdelacruz
CoordinatorLayout或者android:fitsSystemWindows="true"对我都不起作用。 - Alif Hasnain

23

你需要在样式中添加此属性才能看到内容

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

2
这对我有用。这应该被接受为答案。 - abdul rehman

19
 <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


12
将此添加到您的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);

1
谢谢,但这不起作用。状态栏看起来一样。 - pt2121
请帮忙解决这个问题...我也遇到了同样的问题....目前为止没有任何解决方案有效。 - Saeed Jassani
@SaeedJassani 我找到了一个对我有效的解决方案。如果你有同样的问题,请看一下我的问题和答案:stackoverflow.com/a/33892569/1820644 - kirtan403

10

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

true

它应该看起来像这样:

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


1
在最后两个项目中使用“false”值对我有效。很奇怪,但还是谢谢。 - JCarlosR
它只适用于最后两行,但内容会穿过状态栏,padding/margin或fitsSystemWindows无法解决。在运行nougat的OP 5上进行了测试。 - Anton Makov

9
尝试了以上所有方法均未成功后,我发现明确设置主题可以解决该问题。
setTheme(R.style.AppTheme);

在您的活动中,这必须在super.OnCreate()之前进行。

1
我可以确认这个修复了Marshmallow上的错误活动。 - dare0021
工程师相关内容翻译:兄弟,你太棒了,完美解决了问题,虽然我还是不知道为什么会这样 :@ - Adeel Ahmad
在super.OnCreate()之前是关键。 - Karthik Rk

9
我遇到了同样的问题。我的解决方法是,在“v21/styles.xml”文件中将true改为了:
 <item name="android:windowDrawsSystemBarBackgrounds">true</item>

to:

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

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

你会在 values/styles/styles.xml(v21) 文件中看到这行代码。删除它即可解决问题。 此答案的链接

5

我通过将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-viewer)ViewInspector。这些工具可能会对您有所帮助。


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