状态栏变白,后面不显示内容

Status bar turns white and does not show content behind it

我正在 Marshmallow 上试用 AppCompat。我想要一个透明的状态栏,但它会变成白色。我已经尝试了几个解决方案,但它们对我不起作用 (, )。这是相关代码。

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

Activity

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

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

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

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

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

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

此问题的解决方法是添加几乎不需要 space 并且可以不可见的视图。请参阅下面的结构。

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

whosebug.com 上的这个问题也提到了这个错误:

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

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

setTheme(R.style.AppTheme);

必须在 activity 中的 super.OnCreate() 之前。

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

我遇到了完全相同的问题。不知何故,有些活动是正常的,而我创建的新活动显示白色状态栏而不是 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 上测试

对我来说,它通过执行以下操作起作用:

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

本质上是在colorPrimaryDark中绘制状态栏的第三步,否则如果使用NoActionBar主题则不会绘制。 第二步会给你的工具栏覆盖

我在这个link中找到了答案:

所以我们需要删除

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

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

我不确定是否晚了,但我希望它对某人有所帮助。 * 制作一个适合布局的具有透明背景的假视图,并制作一个 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

只需将此项目放入您的 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>

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

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

至:

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

更好的方法

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

只需从样式 v21 中删除以下标签 @android:color/transparent

这对我有用。

 <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

Just Replace this , statusBarColor should be your expected color and not TRANSPARENT

对于样式 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>

[

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

打开 styles.xml (v21)

使用以下任何一种方式

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

如果您正在使用 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"
    >
    ...

写得不错!

如果你的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>

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

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

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

Link to this answer

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

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

我已经找到解决方案 -

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

将此添加到您的风格中,它将适用于 api 21 岁或以上。

    Window window = this.getWindow();
    window.setStatusBarColor(this.getResources().getColor(R.color.colorPrimaryDark));

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

 <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' 项目

将此添加到您的 style.xml

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

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

你想要这个吗?

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

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

你知道,不匹配 apptheme - parent

祝你好运

onCreate 内添加以下内容:

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

及以下

 setContentView(R.layout.activity_main);