如何在底部导航视图中添加分隔线

时间:2017-03-29 05:57:41

标签: android menuitem bottomnavigationview

我正在尝试在 BottomNavigationView

的菜单项中添加divider

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

底部导航视图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" />

感谢您的支持。

3 个答案:

答案 0 :(得分: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>

然后将其添加到NavigationView,作为 app:itemBackground =&#34; @ drawable / item_bg&#34;

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

答案 1 :(得分: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)

    }

注意:单击该项目时,您将在底视图导航中失去波纹效果。

答案 2 :(得分:-1)

试试这种方式......

BottomNavigationView mBottomNavigationView = (BottomNavigationView) findViewById(R.id.navigation);
NavigationMenuView navMenuView = (NavigationMenuView) mBottomNavigationView.getChildAt(0);
navMenuView.addItemDecoration(new DividerItemDecoration(MainActivity.this,DividerItemDecoration.VERTICAL));