浮动操作按钮未显示在Android协调器布局中

问题描述

我是Android和UI开发的新手。我已经下载了Windows Android Studio的最新版本,并做了一些基本的本机UI,效果很好。现在,我正在尝试开发浮动操作按钮UI。

我在CoordinatorLayout添加ConstraintLayout。 FAB被添加到CoordinatorLayout中。我的FAB在布局预览中不可见。

我在该布局中看到androidx.coordinatorlayout.widget.CoordinatorLayout带有背景色的文本。请让我知道如何解决此问题

实施'com.google.android.material:material:1.2.0'

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

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#F44336"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        tools:ignore="MissingConstraints">
        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:backgroundTint="@color/colorPrimary"
            android:src="@drawable/ic_baseline_add_24"
            android:layout_gravity="bottom|end"
            />

    </androidx.coordinatorlayout.widget.CoordinatorLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

enter image description here

enter image description here

解决方法

尝试

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#F44336"
        android:layout_gravity="bottom"
        tools:ignore="MissingConstraints">

        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:layout_width="104dp"
            android:layout_height="107dp"
            android:layout_gravity="center"
            android:foregroundGravity="center"
            android:visibility="visible"
            android:src="@drawable/ic_baseline_add_24"
            app:backgroundTint="@color/colorPrimary" />

    </androidx.coordinatorlayout.widget.CoordinatorLayout>

</LinearLayout>
,

为您的constraintTop添加CoordinatorLayout,以填满整个屏幕

app:layout_constraintTop_toTopOf="parent"

或者甚至会更好

android:layout_width="match_parent"
android:layout_height="match_parent"

删除所有约束

为了进行测试,您还可以添加固定大小的FloatingActionButton。如果您想保留wrap_content用于FAB,请确保ic_baseline_add_24是有效的图标,并且尺寸合适

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#F44336"
        tools:ignore="MissingConstraints">
        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:backgroundTint="@color/colorPrimary"
            android:src="@drawable/ic_baseline_add_24"
            android:layout_gravity="bottom|end"
            />

    </androidx.coordinatorlayout.widget.CoordinatorLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
不需要上面的代码ConstraintLayout中的

,如果您不打算进行一些更复杂的布局,则可以将其删除。 RecyclerView可以放在FAB之前的CoordinatorLayout内-首先绘制REcyclerView,之后再将FAB放在回收站顶部

,

不要将CoordinatorLayoutandroid:layout_height="wrap_content"一起使用以仅包含FAB:

<androidx.coordinatorlayout.widget.CoordinatorLayout
    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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rvItems"
        android:scrollbars="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        app:layout_behavior="com.google.android.material.behavior.HideBottomViewOnScrollBehavior"
        android:layout_margin="@dimen/fab_margin"
        app:srcCompat="@drawable/ic_add_24px" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

enter image description here