如何将BottomAppBar + FAB与BottomNavigationView结合起来

2024-01-12

我想使用FloatingActionButton,以及它锚定在 BottomNavigationView 顶部的 BottomAppBar 上时的行为。

我想出了一个相当“hacky”的技巧来放置BottomNavigationView在 BottomAppBar 顶部,不提供背景,从而使其透明。

乍一看似乎效果很好,但我发现只有在触摸按钮的上半部分时才能单击 fab 按钮(所以在没有透明的地方BottomNavigationView在上面)。

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

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="120dp"
        android:layout_gravity="bottom"
        app:layout_constraintBottom_toBottomOf="parent">

        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:clickable="true"
            android:focusable="true"
            app:layout_anchor="@id/bar" />

        <com.google.android.material.bottomappbar.BottomAppBar
            android:id="@+id/bar"
            android:layout_width="match_parent"
            android:layout_height="58dp"
            android:layout_gravity="bottom"
            android:backgroundTint="@color/colorPrimaryDark" />

        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/bottomNavigation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            app:itemIconTint="@android:color/darker_gray"
            app:itemTextColor="@android:color/white"
            app:labelVisibilityMode="labeled"
            app:menu="@menu/navigation" />

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

有什么方法可以实现这个想法,我可以完全点击FloatingActionButton?


有一种更简单的方法来组合这 3 个小部件。我们可以有这样的东西:

<androidx.coordinatorlayout.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/coordinator"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.fragment.app.FragmentContainerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="?attr/actionBarSize" />

    <com.google.android.material.bottomappbar.BottomAppBar
        android:id="@+id/bottomBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        app:backgroundTint="?attr/colorPrimary"
        app:contentInsetEnd="0dp"
        app:contentInsetStart="0dp"
        app:fabAlignmentMode="end">

        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/bottomNavigation"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:backgroundTint="@android:color/transparent"
            app:elevation="0dp"
            android:layout_marginEnd="100dp"
            app:itemIconTint="@android:color/white"
            app:itemRippleColor="@android:color/white"
            app:itemTextColor="@android:color/white"
            app:menu="@menu/menu_activity_home_bottom_navigation" />

    </com.google.android.material.bottomappbar.BottomAppBar>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:src="@drawable/ic_add_white_24dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_anchor="@id/bottomBar" />


</androidx.coordinatorlayout.widget.CoordinatorLayout>

就是这样。

根据 FAB 的锚定位置 (CENTER|END),菜单图标将放置在左侧或中间:

For this sample, the result would be something like: enter image description here

如果您认为有更好的方法,请随时编辑/评论,以便我修复该帖子:)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何将BottomAppBar + FAB与BottomNavigationView结合起来 的相关文章

随机推荐

  • 我可以使用 Perl 正则表达式来匹配平衡文本吗?

    我想在 Perl 中匹配括号等中包含的文本 我怎样才能做到这一点 这是来自官方常见问题解答 http faq perl org We re 将 perlfaq 导入 Stack Overflow https meta stackexchan
  • print() / println() 执行速度慢吗?

    我有一个有几千行的应用程序 在该代码中有很多 println 命令 这会减慢应用程序的速度吗 它显然是在模拟器中执行的 但是当您从应用程序商店 TestFlight 存档 提交和下载应用程序时会发生什么 这段代码仍然是 活动的 吗 被 注释
  • 对于访问创建它的脚本的每个用户来说,mysql临时表是唯一的...?

    当用户在特定日期之间搜索免费酒店时 在寻找一种临时保存搜索结果的方法时 我遇到了临时表 但某些问题即使在 mysql 手册中也没有得到解答 比如 临时表对于执行脚本的每个用户来说都是唯一的吗 或者当两个不同的用户同时运行脚本时它会被覆盖 桌
  • 将 Excel 文件从压缩文件夹读入 R 数据帧

    我有一个 Excel 文件 xls 扩展名 位于一个压缩文件夹内 我想将其作为数据帧读取到 R 中 我加载了 gdata 库 并将工作目录设置为包含压缩文件夹的文件夹 当我输入以下语法时 data frame1 lt read xls un
  • Azure Devops YAML 管道 - 如何重复任务

    在我的 YAML 管道中 我有一个部署作业 stage deployment to development jobs deployment deployment to development displayName Deploy to De
  • 来自不同插件的重复控制器名称

    我有一个关于 Cakephp2 3 的文件加载系统的问题 我有两个插件 让我们称它们为 联系人 和 经理 加载如下 CakePlugin load Contacts CakePlugin load Managers 它们每个都有一个名为 D
  • DBCP 连接池登录超时

    根据DBCP文件 http commons apache org dbcp apidocs org apache commons dbcp BasicDataSource html setLoginTimeout 28int 29 Basi
  • 如何将包含连字符的键的对象解构为变量? [复制]

    这个问题在这里已经有答案了 如何从键包含连字符的对象解构属性 Eg accept ranges bytes cache control public max age 0 content length 1174 content type ap
  • 为 Chrome 扩展注入 CSS

    我对 Chrome 扩展开发还很陌生 我知道可以注入CSS 但是是否可以为特定的 URL 注入它 例如 每当我访问 google com 时 CSS 就会被注入 谢谢您的帮助 那么 您有 2 个选择 编程注入和内容脚本 这些名字可能听起来非
  • 将 csv 文件导入 java swing 表

    我有纽约证券交易所所有股票报价的 csv 文件 第一列是符号第二列是公司名称 我有一个使用 java swing 库在 netbeans 中制作的搜索框和表格 现在 当我在框中输入名称时 它会返回正确的行数 例如 如果我搜索 GOOG 它只
  • 根据调整窗口大小的动态高度 div

    HTML div class header Header div div class body table class body table tr td Cell td td Cell td td Cell td tr tr td Cell
  • 如何在for循环中声明变量? (IDL)

    例如 我的文件以00 dat 01 dat 02 dat 每个文件包含多个列 我使用READCOL将它们读入变量 for i 0 n 1 do begin readcol string i F I02 dat F D D a0 b0 rea
  • postgresql中如何计算空值?

    select distinct column from table output column 1 0 0 2 null 3 1 0 但是当我尝试计算空值时 select count column from train where colu
  • 直接访问子类中自动合成的实例变量?

    为了提高效率 我想访问与子类中的属性关联的成员变量 如果我有一个声明如下的财产 interface Mumbo NSObject property nonatomic GLKVector3 position end 在 Mumbo 的实现中
  • 使用 Win32 API 连接字符串

    使用 Win32 连接字符串的最佳方法是什么 如果理解正确 正常的 C 方法是使用strcat 但由于 Win32 现在处理 Unicode 字符串 又名LPWSTR 我想不出办法strcat来处理这个 有这个功能吗 还是我应该自己写 ls
  • 从分页 URL 中删除“页面”

    我在更改 Wordpress 中的分页 URL 时遇到问题 我知道这个问题的通用解决方案是更改 WordPress 核心文件 但我只需要针对一个类别使用此解决方案 也许只有一个类别可以通过 htaccess 来完成 现在有这样的网址 htt
  • 未找到架构 x86_64 的符号 - Cmake - Mac sierra

    最近我开始了一个 C 的新项目 问题是 当我尝试编译它时 出现链接错误 我今天花了一整天的时间尝试调试它 但我并没有真正在任何地方找到好的解决方案 如果有人能帮忙那就太好了 我使用的是 Mac Sierra parsing methylat
  • 如何通过 EF Model First 数据库架构升级避免数据丢失?

    这是一个很长的问题 但如果我能就此得到一些好的建议 我将非常非常感激 简而言之 我正在寻找一种对 MS SQL 数据库模式进行版本升级的好方法 该方法还要求将数据从已删除的表移至新表 我认为 Stack Overflow 是解决这个问题最合
  • 如何在 Emacs 中输入希腊字符

    This http xahlee info emacs emacs emacs n unicode htmlpage 表明希腊字母可以通过使用插入到 Emacs 中M i 然而 Debian Squeeze 变体中的 Emacs 23 2
  • 如何将BottomAppBar + FAB与BottomNavigationView结合起来

    我想使用FloatingActionButton 以及它锚定在 BottomNavigationView 顶部的 BottomAppBar 上时的行为 我想出了一个相当 hacky 的技巧来放置BottomNavigationView在 B