10

我正在尝试在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" />

谢谢你的支持。

4

3 回答 3

11

像这样创建一个可绘制的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:itemBackground="@drawable/item_bg"添加到 NavigationView

 <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" />
于 2017-03-31T11:55:52.690 回答
0

如果有人仍然有兴趣这样做,尽管不建议这样做。您可以以编程方式实现这一点,以避免像公认的解决方案那样在屏幕末尾设置分隔符。

 val bottomNavigationMenuView = (binding.navView[0] as BottomNavigationMenuView)
    if (bottomNavigationMenuView.isNotEmpty()) {
                  bottomNavigationMenuView[1].setBackgroundResource(R.drawable.bottom_view_divider)
        bottomNavigationMenuView[2].setBackgroundResource(R.drawable.bottom_view_divider)
        bottomNavigationMenuView[3].setBackgroundResource(R.drawable.bottom_view_divider)

    }

注意:单击该项目时,您将失去底部视图导航中的涟漪效果。

于 2020-11-06T00:16:43.363 回答
-4

试试这个方法...

BottomNavigationView mBottomNavigationView = (BottomNavigationView) findViewById(R.id.navigation);
NavigationMenuView navMenuView = (NavigationMenuView) mBottomNavigationView.getChildAt(0);
navMenuView.addItemDecoration(new DividerItemDecoration(MainActivity.this,DividerItemDecoration.VERTICAL));
于 2017-03-29T06:04:07.657 回答