首页 > 解决方案 > fitSystemWindows 与 windowTranlucentStatus 和 windowTranslucentNavigation 使工具栏太高

问题描述

我试图让我的应用在状态栏和导航栏下流血。我使用 fitSystemWindows = true 使布局工作,只有状态栏是半透明的,但是当我也制作导航栏时,工具栏似乎会做出反应,就好像它需要在状态栏和通知栏下填充一样。 出于某种原因,工具栏在横向模式下似乎可以正常工作。 这种双“半透明”的条形效果也使得 NestedScrollView 延伸到了页面底部之外。我也不确定如何使 FAB 适合。

另一方面,我最初使用 CoordinatorLayout 编写布局。这增加了一个额外的问题,即 NestedScrollView 然后向上滚动到工具栏应位于的底部,而不是它所在的位置。但是,我认为 NestedScrollView 使用 CoordinatorLayout 正确尊重了底部屏幕边界。

这是布局的xml:

<?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:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.constraint.ConstraintLayout
        android:id="@+id/constraint"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v4.widget.NestedScrollView
            android:id="@+id/nested_scroll_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toBottomOf="@id/toolbar">

            <LinearLayout
                android:id="@+id/parent"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#00000000"
                android:clipToPadding="false"
                android:orientation="vertical"
                android:paddingBottom="74dp"/>

        </android.support.v4.widget.NestedScrollView>

        <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"
            app:layout_constraintTop_toTopOf="parent"
            android:background="@color/nav_color_1_bright"
            android:elevation="4dp"
            android:fitsSystemWindows="true"

            app:navigationContentDescription="@string/open_nav_menu"
            app:navigationIcon="@drawable/ic_dehaze_white_24dp"/>


        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            android:layout_margin="16dp"
            android:clipToPadding="false"
            android:fitsSystemWindows="true"
            android:onClick="createNew"
            android:src="@drawable/ic_add_black_24dp"
            app:backgroundTint="@color/darkAccent"
            app:fabSize="normal" />

    </android.support.constraint.ConstraintLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header"
        app:itemBackground="@drawable/nav_item">

        <ListView
            android:id="@+id/drawer_list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:divider="@null"
            android:dividerHeight="0dp"
            android:fitsSystemWindows="true" />
    </android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>

我以编程方式将以下“轻主题”应用于活动。

<style name="AppTheme" parent="Theme.AppCompat.DayNight.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:windowTranslucentStatus">true</item>
    <item name ="android:windowTranslucentNavigation">true</item>
</style>

<style name="LightTheme" parent="AppTheme">
    <item name="android:colorPrimary">@color/lightPrimary</item>
    <item name="android:colorPrimaryDark">@color/lightPrimaryDark</item>
    <item name="android:colorAccent">@color/lightAccent</item>
    <item name="android:navigationBarColor">@color/lightPrimaryDark</item>
    <item name="actionOverflowButtonStyle">@style/OverflowMenuButtonStyleDark</item>
</style>

这是布局的预览:(此处为大版本)

工具栏位于状态栏下方,并认为它也需要位于导航下方

标签: android

解决方案


推荐阅读