其他分享
首页 > 其他分享> > android – 解决:如何在底部导航视图中添加分隔符

android – 解决:如何在底部导航视图中添加分隔符

作者:互联网

我正在尝试在BottomNavigationView的菜单项中添加分隔符

所有项目都在底部水平显示,但我无法在其中添加分隔符.

底部导航视图xml:

<android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:focusable="false"
        app:itemBackground="@color/colorPrimaryDark"
        app:itemIconTint="@color/bottom_navigation_selector"
        app:itemTextColor="@color/bottom_navigation_selector"
        app:layout_anchor="@id/container"
        app:layout_anchorGravity="bottom"
        app:menu="@menu/bottom_nav_items" />

和bottom_nav_items.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:id="@+id/menu_family"
    android:checked="false"
    android:icon="@drawable/family"
    android:title="@string/family" />
<item
    android:id="@+id/menu_me"
    android:checked="false"
    android:icon="@drawable/me"
    android:title="@string/me" />

<item
    android:id="@+id/menu_blank"
    android:checkable="false"
    android:enabled="false"
    android:title="" />
<item
    android:id="@+id/menu_event"
    android:checked="false"
    android:icon="@drawable/event"
    android:title="@string/event" />
<item
    android:id="@+id/menu_more"
    android:checked="false"
    android:icon="@drawable/more"
    android:title="@string/more" />

谢谢你的支持.

解决方法:

像这样创建一个drawable item_bg.xml,

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item>
        <shape android:shape="rectangle" >
            <solid android:color="#F4F4F4" />
        </shape>
    </item>

    <item android:top="-2dp" android:right="-2dp" android:left="-2dp">
        <shape>
            <solid android:color="@android:color/transparent" />
            <stroke
                android:width="1dp"
                android:color="#EAEAEA" />
        </shape>

    </item>

</layer-list>

然后将其作为app添加到NavigationView:itemBackground =“@ drawable / item_bg”

 <android.support.design.widget.BottomNavigationView
    android:id="@+id/navigation"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:focusable="false"
    app:itemBackground="@drawable/item_bg"
    app:itemIconTint="@color/bottom_navigation_selector"
    app:itemTextColor="@color/bottom_navigation_selector"
    app:layout_anchor="@id/container"
    app:layout_anchorGravity="bottom"
    app:menu="@menu/bottom_nav_items" />

标签:bottomnavigationview,android,menuitem
来源: https://codeday.me/bug/20191004/1853754.html