首页 > 解决方案 > 浮动操作按钮未显示在 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>

在此处输入图像描述

在此处输入图像描述

标签: androidandroid-layoutandroidxfloating-action-button

解决方案


尝试

<?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>

推荐阅读