透明状态栏不适用于 windowTranslucentNavigation="false"

Transparent status bar not working with windowTranslucentNavigation="false"

我正在开发 Activity,我需要在 运行 5.0+(API 21+)设备上使导航栏不透明,状态栏透明。下面是我使用的样式,以及对我的问题的解释。

AppTheme 扩展 Theme.AppCompat.Light.NoActionBar

<item name="android:statusBarColor">@color/transparent</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">@color/welbe_red_transparent</item>

FullscreenTheme 扩展 AppTheme

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

这使得应用看起来像这样

如果我删除 android:windowTranslucentNavigation 样式,或将其设置为 Fullscreen 中的 false,它会修复导航栏问题。问题是状态栏变成完全白色而不是保持透明并显示其后面的内容。

我试过在布局中使用 fitsSystemWindow="true",但没有解决问题。有人知道为什么会这样吗?

据我所知,在 API 小于 19 时,没有正确的方法来设置状态栏的颜色。

对于 API 19+,您可以仅对状态栏使用与 windowTranslucentNavigation 类似的属性:

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

备注:

  • 您收到白色状态栏的原因是

    <item name="android:statusBarColor">@color/transparent</item> 
    
  • some hacks 适用于特定制造商的设备,但我自己不会使用它们。

android:windowTranslucentNavigation 做了一件 android:statusBarColor 没有做的事情,那就是请求 SYSTEM_UI_FLAG_LAYOUT_STABLESYSTEM_UI_FLAG_LAYOUT_FULLSCREEN 标志。

为了在状态栏后面绘制,您需要请求这些。

在您的 ActivityonCreate 中请求它们:

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

或者,您也可以简单地设置您的应用程序主题背景,它也会在您的状态栏后面弹出。

更多信息

我为此苦苦挣扎了 3 个多小时。将所有内容包装在 CoordinatorLayout 中,它似乎是唯一关注 fitsSystemWindow="true" 或 "false"

的布局

这是我的主要activity片段

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/content_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include layout="@layout/layout_google_map"/>

    <include layout="@layout/layout_toolbar_transparent"/>

    <include layout="@layout/layout_action_buttons"/>

    <include layout="@layout/layout_bottom_sheet"/>

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

这是我的工具栏布局

<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.v7.widget.Toolbar
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        android:theme="@style/ToolbarTheme"
        app:popupTheme="@style/AppTheme.PopupOverlay">

        <android.support.v7.widget.AppCompatImageView
            android:layout_width="wrap_content"
            android:layout_height="?actionBarSize"
            android:layout_centerHorizontal="true"
            android:layout_gravity="center"
            android:adjustViewBounds="true"
            app:srcCompat="@drawable/ic_fynd_logo_red"/>

    </android.support.v7.widget.Toolbar>
</android.support.design.widget.CoordinatorLayout>

如您所见,我的片段布局使 google 地图绘制在状态栏下方。

我有一个操作栏,公司徽标所在的位置。

其他 ui 按钮 "layout_action_buttons" 也包裹在 Coordinator 布局中,因此 fitsSystemsWindows 可以正常工作。

检查一下。

在任何 activity

中实现此目标
  1. 添加样式:
 <style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
                <item name="colorPrimary">@color/colorPrimary</item>
                <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
                <item name="colorAccent">@color/colorAccent</item>
                <item name="android:statusBarColor">@android:color/transparent</item>
                <item name="android:windowBackground">@android:color/white</item>
            </style>
  1. 在XML

  2. 中使用CoordinatorLayout作为根布局
  3. 在oncreate()方法中,setcontentview之前使用

    window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE 或 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

对于最新的 android 版本 R 和向后兼容性,这对我有用。

WindowCompat.getInsetsController(window, window.decorView)?.isAppearanceLightStatusBars =false /* true for dark icon on StatusBar and false for white icon on StatusBar */
WindowCompat.setDecorFitsSystemWindows(window, false)
WindowCompat.getInsetsController(window, window.decorView)?.show(WindowInsetsCompat.Type.statusBars())
ViewCompat.setOnApplyWindowInsetsListener(findViewById(android.R.id.content)) { rootView, insets ->
    val navigationBarHeight = insets.getInsets(WindowInsetsCompat.Type.navigationBars()).bottom
    rootView.setPadding(0, 0, 0, navigationBarHeight)
    insets
}

对于Fragment,您可以使用activity?.window...