首页 > 解决方案 > Android中BottomNavigationView的分隔符

问题描述

如何在 Android 中为 BottomNavigationView 提供分隔符?我需要 Home 和 Settings 之间的这条细线。 在此处输入图像描述

标签: javaandroidxmlandroid-layout

解决方案


您可以按照此处的建议为BottomNavigationView创建背景。如果您已经有背景并且由于BottomNavigationViewFrameLayout,则可以添加一个分隔符,如下所示:

navigation_divider.xml

<shape
    android:shape="rectangle">
    <size android:width="2dp" />
    <solid android:color="#E91E63" />
</shape>

activity_main.xml

<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="?attr/actionBarSize">

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/nav_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="0dp"
        android:layout_marginEnd="0dp"
        android:background="?android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/bottom_nav_menu">

    <View
        android:layout_width="2dp"
        android:layout_height="30dp"
        android:layout_gravity="center"
        android:background="@drawable/navigation_divider" />
    </com.google.android.material.bottomnavigation.BottomNavigationView>

    <fragment
        android:id="@+id/nav_host_fragment_activity_main"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@id/nav_view"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/mobile_navigation" />

</androidx.constraintlayout.widget.ConstraintLayout>

在此处输入图像描述

当然,您需要在菜单中选择偶数个选项。


推荐阅读