如何在底部导航栏中添加浮动图标?

时间:2020-03-28 16:54:30

标签: android material-design bottomnavigationview

如何为底部导航栏中的中心图标实现类似的功能?

 buttom

2 个答案:

答案 0 :(得分:0)

有一个名为BottomAppBar的组件,可以帮助您实现这一目标。您需要将此依赖项添加到build.gradle文件中:

implementation 'com.google.android.material:material:1.1.0'

然后您的代码应如下所示:

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

    <include layout="@layout/your_layout_content" />

    <com.google.android.material.bottomappbar.BottomAppBar
        android:id="@+id/bottomAppBar"
        style="@style/Widget.MaterialComponents.BottomAppBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        app:backgroundTint="@color/colorPrimary"
        app:fabAlignmentMode="center"
        app:fabCradleMargin="10dp"
        app:fabCradleRoundedCornerRadius="10dp"
        app:fabCradleVerticalOffset="5dp"
        app:hideOnScroll="true" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:fabSize="normal"
        app:layout_anchor="@id/bottomAppBar"
        app:srcCompat="@drawable/ic_your_icon" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

您的应用程序主题也应从该父项扩展:Theme.MaterialComponents

答案 1 :(得分:0)

我的想法是创建两个浮动按钮,其中一个将完全白色,并且比原始按钮大一点。您将两者都放在底视图顶部的同一位置,看起来就像您的背景视图被裁剪了。

<View
    android:id="@+id/view"
    android:layout_width="match_parent"
    android:layout_height="64dp"
    android:background="#f12"
    app:layout_constraintBottom_toBottomOf="parent" />

<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id="@+id/fab1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="end|bottom"
    android:layout_margin="16dp"
    android:background="@drawable/bkg_circle"
    android:backgroundTint="#fff"
    android:padding="44dp"
    app:layout_constraintHorizontal_bias="0.85"
    app:layout_constraintBottom_toTopOf="@+id/view"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="@+id/view" />

<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="end|bottom"
    android:layout_margin="16dp"
    android:background="@drawable/bkg_circle"
    android:backgroundTint="@color/colorAccent"
    android:padding="12dp"
    android:src="@drawable/ic_menu_offline"
    app:layout_constraintBottom_toBottomOf="@+id/fab1"
    app:layout_constraintEnd_toEndOf="@+id/fab1"
    app:layout_constraintStart_toStartOf="@+id/fab1"
    app:layout_constraintTop_toTopOf="@+id/fab1" />