首页 > 解决方案 > 为什么工具栏在 android 系统状态栏下方(请查看截图)?

问题描述

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

工具栏位于操作系统状态栏下方

那么为什么工具栏标题会出现在操作系统状态栏的下方呢?以及如何使状态栏从操作系统状态栏结束的位置开始?

这是片段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>

标签: androidandroid-layout

解决方案


在您的情况下,将以下链接添加到您的父 xml 标记中CoordinatorLayout。工具栏将出现在状态栏下方。

android:fitsSystemWindows="true"

推荐阅读