fab 按钮在 android 的布局中不可见

The fab button is not visible in layout in android

fab 按钮在布局中不可见。 在不使用 listView 的情况下,fab 按钮是可见的,但是当我添加 listView 时,fab 按钮不可见。

    <?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.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/my_drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    tools:ignore="HardcodedText">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <ListView
            android:id="@+id/contactList"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        </ListView> 
        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_gravity="end|bottom"
            android:layout_marginTop="450dp"
            android:layout_marginRight="20dp"
            android:layout_marginBottom="100dp"
            android:src="@drawable/ic_baseline_add" />
    </LinearLayout> 
    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/navigation_menu" /> 
</androidx.drawerlayout.widget.DrawerLayout>

如何在布局中显示 fab 按钮

尝试使用 CoordinatorLayout:

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.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/my_drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    tools:ignore="HardcodedText">

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ListView
            android:id="@+id/contactList"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_gravity="end|bottom"
            android:layout_marginTop="450dp"
            android:layout_marginRight="20dp"
            android:layout_marginBottom="100dp"
            android:src="@drawable/ic_baseline_add" />

    </androidx.coordinatorlayout.widget.CoordinatorLayout>

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/navigation_menu" /> 
        
</androidx.drawerlayout.widget.DrawerLayout>