【问题标题】:Toolbar overlaps Translucent status bar工具栏重叠半透明状态栏
【发布时间】:2017-02-08 07:05:47
【问题描述】:

在一个活动中我有一个工具栏,它的布局代码是:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.aminiam.moviekade.fragment.AllReviewFragment">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:theme="@style/AppTheme">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />

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

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recReview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/toolbar"/>

</LinearLayout>

我想在我的应用程序中有透明的状态栏,当我将&lt;item name="android:windowTranslucentStatus"&gt;true&lt;/item&gt; 添加到活动主题时,工具栏将与状态栏重叠。

values-v21 style.xml:

<resources>
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:windowBackground">@color/windowBackground</item>
</style>

<style name="TransparentStatusBar" parent="AppTheme">
    <item name="android:windowTranslucentStatus">true</item>
</style>

我在 Stackoverflow 中测试了所有答案,但都无法解决问题。

【问题讨论】:

  • AppBarLayout 主题应该是 `app:theme="@style/AppTheme.AppBarOverlay"` 并将其添加到样式 &lt;style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" /&gt; 并在顶部线性布局中添加 android:fitsSystemWindows="true"
  • @AbhishekSingh Andorid 工作室说:找不到与给定名称匹配的资源(在“主题”处,值为“@style/AppTheme.AppBarOverlay”)。
  • 我也只在评论中添加了你问题的答案

标签: android android-toolbar android-statusbar


【解决方案1】:

添加 android:fitsSystemWindows="true"

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:fitsSystemWindows="true"
        tools:context="com.aminiam.moviekade.fragment.AllReviewFragment">

【讨论】:

  • 这很奇怪,我测试过 android:fitsSystemWindows="true" 但它根本不起作用。现在我重置了我的代码并再次测试我看到它正在工作。谢谢
【解决方案2】:

对我有用的一个方便的技巧是向工具栏元素添加顶部填充。

<android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/transparent"
            android:elevation="4dp"
            android:paddingTop="24dp" //status bar height
            android:fitsSystemWindows="true"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:title="Home">

【讨论】:

    【解决方案3】:

    从 appbarlayout 中移除 android:fitsSystemWindows="true"。

    【讨论】:

    • 从 appbar 中移除并放入工具栏 android:fitsSystemWindows="true"