182

我想在我的活动中使用 appcompat v21 工具栏。但是我正在实现的工具栏在状态栏下方重叠。我该如何解决?

重叠工具栏

这是活动布局xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar" />

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />
</LinearLayout>

工具栏视图:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary" />

主题风格:

<style name="AppTheme" parent="MaterialNavigationDrawerTheme.Light.DarkActionBar">
    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/primary_dark</item>
    <item name="colorAccent">@color/accent</item>
</style>
4

10 回答 10

295

在布局的根视图中使用android:fitsSystemWindows="true"(在您的情况下为 LinearLayout)。并且android:fitsSystemWindows是一个

内部属性根据状态栏等系统窗口调整视图布局。如果为 true,则调整此视图的填充以为系统窗口留出空间。仅当此视图在非嵌入式活动中时才会生效。

必须是布尔值,“真”或“假”。

这也可能是对包含此类型值的资源(形式为“@[package:]type:name”)或主题属性(形式为“?[package:][type:]name”)的引用.

这对应于全局属性资源符号fitsSystemWindows。

于 2015-04-20T02:46:56.247 回答
19

只需将其设置为 v21/styles.xml 文件

 <item name="android:windowDrawsSystemBarBackgrounds">true</item>
 <item name="android:statusBarColor">@color/colorPrimaryDark</item>

并确保

 <item name="android:windowTranslucentStatus">false</item>
于 2016-06-28T12:59:45.477 回答
11

对我来说,问题是我从一个例子中复制了一些东西并使用了

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

只是删除这个解决了我的问题。

于 2016-06-13T04:07:29.780 回答
10

没有一个答案对我有用,但这是我设置后最终奏效的:

android:fitsSystemWindows="false"

在父活动布局文件中,很多地方都没有建议它,但它对我有用并节省了我的时间

于 2018-02-17T17:13:51.660 回答
10

没有一个答案对我有用,但这是我android:fitSystemWindows在根视图上设置后最终起作用的方法(我在样式 v21 中设置了这些):

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

确保您没有AS 默认设置的以下行:

<item name="android:statusBarColor">@android:color/transparent</item>
于 2016-09-04T22:33:03.880 回答
6

我从/res/values-v21/styles.xml中删除了下面提到的所有行,现在它工作正常。

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


 <item name="windowActionBar">false</item>
 <item name="android:windowDisablePreview">true</item>

 <item name="windowNoTitle">true</item>

 <item name="android:fitsSystemWindows">true</item>
于 2016-07-20T06:31:30.150 回答
2

根据谷歌文档,我们不应该在应用主题中使用 fitSystemWindows 属性,它旨在用于布局文件。在主题中使用可能会导致吐司消息出现问题。

在此处检查问题和此处 导致的问题示例

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

使用正确方法的示例,它也适用于windowTranslucentStatus

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"

android:fitsSystemWindows="true"

android:layout_width="match_parent"
android:layout_height="match_parent"
> 


<include layout="@layout/toolbar"
     android:layout_width="match_parent"
     android:layout_height="wrap_content">
</include>

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    android:background="@color/white"
    android:animateLayoutChanges="true"
    app:headerLayout="@layout/navigation_drawer_header"
    app:menu="@menu/navigation_drawer_menu" />

</android.support.v4.widget.DrawerLayout>
于 2018-04-17T09:48:44.963 回答
1

从样式或样式中删除以下行(21)

<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">@color/colorPrimaryDark</item>
<item name="android:windowTranslucentStatus">false</item>
于 2018-09-15T14:28:04.077 回答
0

要防止工具栏与状态栏重叠,请在您的主题中添加这些行

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

它对我有用

于 2022-01-05T15:54:35.973 回答
0

发生这种情况有很多原因..首先你必须检查你的他们是怎么回事..

<style name="AppTheme" parent="BaseAppTheme">
        <item name="android:windowBackground">@android:color/white</item>
        <item name="colorPrimary">@color/primary</item>
        <item name="colorPrimaryDark">@color/primary_dark</item>
        <item name="colorAccent">@color/accent</item>
        <item name="android:textColorHint">@color/secondary_text</item>
    </style>

如果您<item name="android:windowTranslucentStatus">true</item>在主题中使用此代码,它应该与您的操作栏重叠。你可以用它 false 来解决这个问题。
<item name="android:windowTranslucentStatus">false</item>.

或者如果你想保持真实,请使用

<item name="android:fitsSystemWindows">true</item>作为真实。您还有另一个选择来使用它。您可以使用 android:fitsSystemWindows="true"

这在特定 xml 文件的根视图中。

于 2021-09-14T03:17:17.677 回答