自定义工具栏未附加到顶部

Custom toolbar isn't attached to the top

我想创建一个自定义工具栏而不是默认的应用程序工具栏

我将 Theme.AppCompat.Light 更改为 Theme.AppCompat.Light.NoActionBar duo to guides,默认的工具栏内容和颜色消失了,但在屏幕的开头仍然有一个工具栏高度 (space) 并且我的自定义工具栏放在那之后

代码:

MainActivity.kt

val customToolbar = findViewById<androidx.appcompat.widget.Toolbar>(R.id.main_toolbar)
customToolbar.setTitleTextAppearance(this, R.style.DanaBoldTextAppearance)
setSupportActionBar(customToolbar)

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layoutDirection="rtl"
    android:paddingTop="?attr/actionBarSize">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/main_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorAccent"
        android:minHeight="?android:attr/actionBarSize"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        >


    </androidx.appcompat.widget.Toolbar>

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/nav_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="0dp"
        android:layout_marginEnd="0dp"
        android:background="?android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/bottom_nav_menu" />


    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="@color/design_default_color_on_secondary"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@id/nav_view"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/main_toolbar"
        app:layout_constraintVertical_bias="0.0"
        app:navGraph="@navigation/mobile_navigation">


    </androidx.fragment.app.FragmentContainerView>

</androidx.constraintlayout.widget.ConstraintLayout>

styles.xml


<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.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="fontFamily">@font/dana_regular</item>
    </style>
</resources>

您已在主布局中设置 android:paddingTop="?attr/actionBarSize" androidx.constraintlayout.widget.ConstraintLayout 将其移除以解决问题。