操作栏和状态栏之间的间隙,非半透明

Gap between Actionbar and statusbar, non translucent

我在状态栏和操作栏之间有一个间隙,就是无法让它消失。我玩过 android:fitsSystemWindows,下面你会看到它的例子。我通过 AppCompatActivity 使用 supportActionBar。它是使用导航组件设置的:

val navController = navController()
val appBarConfiguration = AppBarConfiguration(setOf(R.id.navigation_onboarding, R.id.projectsFragment, R.id.navigation_menu))
setupActionBarWithNavController(navController, appBarConfiguration)

显示的片段只是简单的 Constaintlayout,没有任何 android:fitsSystemWindows 属性。

关于如何消除这个差距的任何线索?

截图一:- - - - - - - - - - - - - - - - - - - - - - 截图二:

主要activity布局:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/container"
    android:fitsSystemWindows="true" <!--Screenshot 1-->
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <fragment
        android:id="@+id/navigationHostFragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:fitsSystemWindows="true" <!--Screenshot 2-->
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@id/bottomNavigationView"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/navigation" />

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottomNavigationView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="0dp"
        android:layout_marginEnd="0dp"
        android:background="?android:attr/windowBackground"
        android:visibility="gone"
        app:itemIconTint="@color/bottom_navigation"
        app:labelVisibilityMode="unlabeled"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/bottom_nav_menu" />

</androidx.constraintlayout.widget.ConstraintLayout>

主题:

<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
    ...
    <item name="android:windowTranslucentStatus">false</item>
    <item name="android:windowTranslucentNavigation">false</item>
</style>

我们最终通过添加自定义工具栏并启用半透明状态栏解决了这个问题:

toolbar.adjustHeightUnderStatusBar(this)
setSupportActionBar(toolbar)

使用此视图布局

<androidx.constraintlayout.widget.ConstraintLayout 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/container"
    android:fitsSystemWindows="true"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@color/grey"
        android:elevation="4dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:title=""
        app:titleTextColor="@color/darkBlue"
        tools:ignore="HardcodedText" />

    <fragment
        android:id="@+id/navigationHostFragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@id/bottomNavigationView"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/toolbar"
        app:navGraph="@navigation/navigation" />

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottomNavigationView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="0dp"
        android:layout_marginEnd="0dp"
        android:background="?android:attr/windowBackground"
        android:visibility="gone"
        app:itemIconTint="@color/bottom_navigation"
        app:labelVisibilityMode="unlabeled"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/bottom_nav_menu" />

</androidx.constraintlayout.widget.ConstraintLayout>

adjustHeightUnderStatusBar 函数:

fun Toolbar.adjustHeightUnderStatusBar(activity: BaseActivity) {
    val statusBarHeight = 24.px
    var actionBarHeight = 48.px
    val value = TypedValue()
    if (activity.theme.resolveAttribute(android.R.attr.actionBarSize, value, true)) {
        actionBarHeight = TypedValue.complexToDimensionPixelSize(value.data, resources.displayMetrics)
    }

    val layoutParams = layoutParams as ConstraintLayout.LayoutParams
    layoutParams.height = actionBarHeight + statusBarHeight
    this.layoutParams = layoutParams
    setTopPadding(statusBarHeight)
}