【问题标题】:Floating Action button is not showing in Android Coordinator Layout浮动操作按钮未显示在 Android 协调器布局中
【发布时间】:2020-12-13 18:41:48
【问题描述】:

我是 Android 和 UI 开发的新手。我已经下载了最新版本的 Windows Android Studio 并做了一些基本的原生 UI,它运行良好。现在,我正在尝试开发浮动操作按钮 UI。

我在ConstraintLayout 中添加了CoordinatorLayout。 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>

【问题讨论】:

  • 您在 CoordinatorLayout 中缺少顶部约束。 CoordinatorLayout 也应该是 Root View 才能使用它。
  • 尝试给协调器布局的 layout_height 和 layout_width 0dp。确保您还设置 FAB 并提供 app:layout_anchor
  • 我打算添加 RecycleView 布局是顶部和底部是 FAB 页面
  • 不走运。最好给我完整的更正码
  • 问题与android:src="@drawable/ic_baseline_add_24"有关,请将其更改为适当的drawable,然后它可以正常工作,或者您可以将其删除。

标签: android android-layout androidx floating-action-button


【解决方案1】:

不要使用 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>

【讨论】:

  • 不走运。看不到 Recyleview 和 FAB 按钮 :(
  • 我复制了你的代码,没有看到上面的结果。我不知道为什么?你知道为什么我不能在最新的 Android Studio 中添加一个 FAB
  • @Gnana 不知道,抱歉。
  • 我重新启动了几次,并且能够在不在 Layout 中的虚拟设备中看到正确的输出。我不知道为什么
【解决方案2】:

试试

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

【讨论】:

  • 不走运。同样的结果:(
  • android:src = "@drawable/ic_baseline_add_24" 引用来自应用程序的图像源 --> res --> drawable
  • 您希望通过该 FAB 实现什么目标?
  • 与问题无关,但不要在FAB中使用android:backgroundTint。使用app:backgroundTint
  • @Mdbasha : 不走运,我现在看到 CoordinatorLayout 有全高,看不到 FAB,
【解决方案3】:

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

app:layout_constraintTop_toTopOf="parent"

甚至这样会更好

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

移除所有约束

为了测试,您还可以添加固定大小的FloatingActionButton。如果您想为 FAB 保留 wrap_content,请确保 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 可以放在 CoordinatorLayout 中,就在 FAB 之前 - 首先将绘制 REcyclerView,然后在回收器顶部的 FAB 之后

【讨论】:

  • 不走运,我现在看到 CoordinatorLayout 有全高,看不到 FAB,
  • 你看到CoordinatorLayout背景色#F44336全屏了吗?
  • 是的,我看到 CoordinatorLayout 背景颜色 #F44336 全屏
  • FloatingActionButton 固定80dp 大小不可见?尝试用layout_widthlayout_height (都设置为固定大小)和一些颜色来识别它是否存在
猜你喜欢
  • 2021-12-11
  • 2017-06-14
  • 2016-11-09
  • 2016-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-18
  • 1970-01-01
相关资源
最近更新 更多