底部导航与浮动操作按钮(fab)

16

我目前正在使用BottomNavigationViewFloatingActionButton

我想要实现如下设计:

enter image description here

而我已经尝试过的:

<?xml version="1.0" encoding="utf-8"?>

<android.support.design.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"
    app:layout_insetEdge="bottom"
    tools:context=".activity.BottomNavPrimary">

    <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center|bottom" />

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/bottomNavPrimary"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        app:layout_insetEdge="bottom"
        android:background="?android:attr/windowBackground"
        app:menu="@menu/bottom_nav_primary"></android.support.design.widget.BottomNavigationView>

</android.support.design.widget.CoordinatorLayout>

1
请查看这里的新“BottomBar”链接,以及设计页面 - Suleyman
5个回答

10

看起来您想使用新的MaterialComponents中即将在Android P中完全推出的BottomAppBar。特别是如果左侧图标代表某种侧边导航,则它可能是正确的导航元素。

但是,您必须注意,与底部导航不同,FAB左右两侧的元素具有与之不同的目的。 BottomAppBar被定义为:

底部应用栏显示移动屏幕底部的导航和关键操作。

因此,它适用于页面操作(例如打开仪表板或搜索)。更多说明可以在设计文档中找到。

我还没有机会实现它(因为我真的需要与底部导航一起使用),但这里是代码文档示例:

<android.support.design.widget.CoordinatorLayout
    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">

  <!-- Other components and views -->

  <com.google.android.material.bottomappbar.BottomAppBar
      android:id="@+id/bar"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_gravity="bottom"
      app:navigationIcon="@drawable/ic_menu_24"/>

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

</android.support.design.widget.CoordinatorLayout>

在我看来,你定义了两个页面菜单选项,由于FAB被固定在工具栏上,它会将它们推向两侧。

文档还包括可选的FAB支架选项,这是今年Google I/O展示的内容,并展示了如何处理菜单和点击处理。

这里还有另一个有用的链接,介绍如何设置gradle来在你的项目中包含新的材料组件。


运行得非常顺利! - Kunwar Shekhar Singh
2
当我们使用操作栏时,无法使用底部应用栏。 - vijay kumar

10

我的解决方案简单而酷。

父布局必须是CoordinatorLayout,并且使用下面所示的BottomAppBar和带有FloatingActionButton

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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">

       <-- your other views -->

    <com.google.android.material.bottomappbar.BottomAppBar
            android:id="@+id/navigation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            app:backgroundTint="@color/colorPrimary">

        <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

            <RelativeLayout
                    android:id="@+id/homePage"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_constraintWidth_percent="0.4">

                <androidx.appcompat.widget.AppCompatImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerInParent="true"
                        android:src="@drawable/your_drawable"
                        android:padding="@dimen/small_margin"
                        app:tint="@color/white" />
            </RelativeLayout>

            <RelativeLayout
                    android:id="@+id/profile"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_constraintWidth_percent="0.4">

                <androidx.appcompat.widget.AppCompatImageView
                        android:layout_width="wrap_content"
                        android:padding="@dimen/small_margin"
                        android:layout_height="wrap_content"
                        android:layout_centerInParent="true"
                        android:src="@drawable/your_drawable" />
            </RelativeLayout>
        </androidx.constraintlayout.widget.ConstraintLayout>

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

    <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/categories"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/your_drawable"
            app:backgroundTint="@color/colorPrimary"
            app:layout_anchor="@id/navigation"
            app:tint="@android:color/white" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

结果:

截图


2
我在想怎么把菜单项放到FloatingActionButton的左边。现在它完美地实现了。 - Fazal Jarral

10

我使用这个,来获得类似的东西。

BottomNavigationView + FloatingActionButton

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

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/floating_button"
        style="@style/Widget.MaterialComponents.FloatingActionButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end"
        android:layout_margin="16dp"
        app:backgroundTint="@color/colorPrimaryLight"
        app:elevation="@dimen/padding_10"
        app:layout_constraintBottom_toTopOf="@id/navigation"
        app:layout_constraintLeft_toRightOf="@id/navigation"
        app:layout_constraintRight_toLeftOf="@id/navigation"
        app:layout_constraintTop_toBottomOf="@id/navigation"
        app:layout_constraintTop_toTopOf="@id/navigation"
        app:layout_insetEdge="bottom"
        app:srcCompat="@drawable/ic_add_black_24dp"
        app:tint="@color/colorPrimary" />


    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@color/colorPrimaryDark"
        android:visibility="visible"
        app:itemIconTint="@drawable/bottom_navigation_icons"
        app:itemTextColor="@drawable/bottom_navigation_icons"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/navigation" />

</androidx.constraintlayout.widget.ConstraintLayout>

如果您想使用导航组件,则 FAB 将无法正常工作。 - Cyd

1
您可以将BottomNavigationViewFloatingActionButton放入ConstraintLayout中,并在FloatingActionButton上使用以下约束条件:
app:layout_constraintTop_toBottomOf="@id/bottomNavPrimary"
app:layout_constraintBottom_toTopOf="@id/bottomNavPrimary"  
app:layout_constraintLeft_toRightOf="@id/bottomNavPrimary"
app:layout_constraintRight_toLeftOf="@id/bottomNavPrimary"

这将使你的FloatingActionButton在垂直和水平方向上相对于你的BottomnavigationView居中。
我希望这就是你所要求的。
最终代码将如下所示:
<android.support.design.widget.CoordinatorLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<android.support.constraint.ConstraintLayout xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@id/bottomNavPrimary"
        app:layout_constraintBottom_toTopOf="@id/bottomNavPrimary"
        app:layout_constraintLeft_toRightOf="@id/bottomNavPrimary"
        app:layout_constraintRight_toLeftOf="@id/bottomNavPrimary"/>

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/bottomNavPrimary"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        app:layout_constraintBottom_toTopOf="parent"
        android:background="?android:attr/windowBackground"
        app:menu="@menu/bottom_nav_primary" />

</android.support.constraint.ConstraintLayout>


@ShuklaSwapnil,是的,您可以使用任何您认为最适合您要求的东西。我的理解是,您想要一个带有中央FAB的BottomBar。BottomAppBar专门用于:访问底部导航抽屉。我从指南中引用了这一点。 - ravi
确保您阅读了这篇文章这篇文章 - ravi
给你的回答点个赞。 - Sumit Shukla
@ShuklaSwapnil 我们有多少个菜单项?如果您只有图片中显示的三个菜单项,它们必须不居中吗? - ravi
2
如果您有两个菜单项,并且它们相对于彼此和FAB居中,那么您可以在中间放置一个空菜单项以实现居中。 - ravi
显示剩余2条评论

1
我认为您可以使用bottomAppBar并在其中嵌入一些按钮,如下所示。
<androidx.coordinatorlayout.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.bottomappbar.BottomAppBar
        android:id="@+id/bottom_app_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        app:backgroundTint="@color/colorPrimary"
        app:fabAlignmentMode="center"
        app:fabAttached="true"
        app:fabCradleDiameter="8dp"
        style="@style/Widget.MaterialComponents.BottomAppBar"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">
            <ImageButton
                android:id="@+id/btnReport"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:background="@android:color/transparent"
                android:src="@drawable/ic_play"
                android:layout_weight="1"
                android:paddingRight="20dp"
                />

            <ImageButton
                android:id="@+id/btnPlayWords"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:background="@android:color/transparent"
                android:src="@drawable/ic_show_chart"
                android:layout_weight="1"
                android:paddingLeft="20dp"
                />
        </LinearLayout>

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

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fabAddWord"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_add"
        app:layout_anchor="@id/bottom_app_bar" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接