状态栏变为白色,并且不显示其背后的内容

pt2*_*121 84 android android-appcompat android-support-library androiddesignsupport android-6.0-marshmallow

我在Marshmallow上尝试AppCompat.我希望有一个透明的状态栏,但它会变成白色.我已经尝试了几个解决方案,但它们对我不起作用(透明状态栏不能使用windowTranslucentNavigation ="false",Lollipop:绘制后面的statusBar,其颜色设置为透明).这是相关的代码.

我的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"/>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

活动

<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>
Run Code Online (Sandbox Code Playgroud)

分段

<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>
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

Pho*_*ang 152

我在此链接中找到了答案:状态栏颜色不随相对布局更改为根元素

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

      <item name="android:statusBarColor">@android:color/transparent</item>
Run Code Online (Sandbox Code Playgroud)

在styles.xml(v21)中.它对我来说效果很好.


小智 94

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

我有同样的问题.不知何故,有些活动是正常的,而我创建的新活动则显示白色状态栏而不是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>
Run Code Online (Sandbox Code Playgroud)

请注意,没有android:fitsSystemWindows="true"这个解决方案对我不起作用.

在棒棒糖和棉花糖上测试过

  • 该解决方案也适用于我.但是......为什么会这样呢?当根为DrawerLayout或CoordinatorLayout时,将显示正确的StatusBar颜色 (3认同)
  • 这只适用于在Android 8中将`android:fitsSystemWindows ="true"`添加到`CoordinatorLayout`中. (3认同)
  • @Franco我认为这是解决方案中最相关的部分.我这样说是因为当我遇到同样的问题时我有`CoordinatorLayout`,并且为我修复此问题的具体属性是`android:fitsSystemWindows ="true"` (2认同)

小智 14

 <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
Run Code Online (Sandbox Code Playgroud)

只需替换它,statusBarColor应该是您期望的颜色而不是TRANSPARENT


Jam*_*ton 9

在尝试了以上所有方法失败后,我发现明确设置主题修复了问题.

setTheme(R.style.AppTheme);
Run Code Online (Sandbox Code Playgroud)

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


小智 9

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

<item name="android:windowLightStatusBar" tools:ignore="NewApi">true</item>
Run Code Online (Sandbox Code Playgroud)

  • 这对我有用。这应该被接受的答案。 (2认同)

Mic*_*rte 8

在style.xml中添加它

    <item name="android:windowTranslucentStatus">true</item>
    <item name="android:statusBarColor">@android:color/transparent</item>
Run Code Online (Sandbox Code Playgroud)

这在你的onCreate();

 getWindow().getDecorView().setSystemUiVisibility(
       View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
Run Code Online (Sandbox Code Playgroud)


小智 8

只需将此项放在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>
Run Code Online (Sandbox Code Playgroud)


Pha*_*res 7

<item name="android:statusBarColor">@android:color/transparent</item>
Run Code Online (Sandbox Code Playgroud)

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

链接到这个答案


小智 6

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

 <item name="android:windowDrawsSystemBarBackgrounds">true</item>
Run Code Online (Sandbox Code Playgroud)

至:

 <item name="android:windowDrawsSystemBarBackgrounds">false</item>
Run Code Online (Sandbox Code Playgroud)


pt2*_*121 5

我通过将我的活动布局从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>
Run Code Online (Sandbox Code Playgroud)

尝试hierarchy-viewerViewInspector.这些工具可能对您有帮助.


She*_*ran 5

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

这对我有用。


bei*_*han 5

我已经找到了解决方案 -

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:windowLightStatusBar">true</item>
</style>
Run Code Online (Sandbox Code Playgroud)

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


归档时间:

查看次数:

56878 次

最近记录:

6 年 前