简体   繁体   中英

Android Toolbar isn't showing up

I use drawer, bottom navigation bar and toolbar in my app.

So I put my toolbar by including layout in drawer layout.

Drawer works well by swiping, but Toolbar isn't showing up.

I think toolbar is covered by fragment,but I don't know why...

I already setSupportActionBar in MainActivity.

What's the problem??

toolbar_layout.xml

<?xml version="1.0" encoding="utf-8"?>
 <androidx.appcompat.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="56dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@android:color/transparent"
android:id="@+id/toolbar"
>

<Button
    android:layout_width="24dp"
    android:layout_height="24dp"
    android:background="@drawable/ic_menu"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    />

<Button
    android:layout_width="24dp"
    android:layout_height="24dp"
    android:background="@drawable/ic_search"
    android:layout_marginEnd="16dp"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    />
  </androidx.appcompat.widget.Toolbar>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer"
android:fitsSystemWindows="true"
>

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

    <FrameLayout
        android:id="@+id/frameLayout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@id/navigationView"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0">

    </FrameLayout>

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/navigationView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/gray7"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:itemBackground="@color/gray7"
        app:labelVisibilityMode="labeled"
        android:theme="@style/Widget.BottomNavigationView"
        app:itemIconTint="@drawable/menu_selector_color"
        app:itemTextColor="@drawable/menu_selector_color"
        app:menu="@menu/bottom_navigationbar" />
</androidx.constraintlayout.widget.ConstraintLayout>

<com.google.android.material.navigation.NavigationView
    android:id="@+id/nvDrawer"
    android:layout_width="304dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    >
    <include layout="@layout/drawer_layout"/>

</com.google.android.material.navigation.NavigationView>


</androidx.drawerlayout.widget.DrawerLayout>

You are setting your FrameLayout containter from the top of the parent view

change this

app:layout_constraintTop_toTopOf="parent"

with

app:layout_constraintTop_toBottomOf="@+id/toolbar"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM