为什么工具栏在android系统状态栏下面(请看截图)?

Why toolbar is getting under the android system status bar (please check the screenshot)?

我收到了一个android项目代码,我需要在片段中添加material工具栏。一切顺利,但我注意到工具栏始终位于屏幕顶部的 OS 状态栏下方,如以下屏幕截图所示:

那么为什么工具栏 header 位于 OS 状态栏下方?以及如何将状态栏推到 OS 状态栏结束的位置?

这里是片段 xml 代码:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.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">

<com.google.android.material.appbar.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/wifi_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/orange_500" />

    <com.google.android.material.tabs.TabLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/watch" />

        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/downloads_item" />

    </com.google.android.material.tabs.TabLayout>
</com.google.android.material.appbar.AppBarLayout>

<androidx.viewpager.widget.ViewPager
    android:id="@+id/wifi_view_pager"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>

您在 activity 中使用 getWindow() 吗? 请删除它并重新测试。

将下面 link 添加到您的父 xml 标签中,在您的例子中是 CoordinatorLayout。工具栏将出现在状态栏下方。

android:fitsSystemWindows="true"