如何在底部导航视图中添加分隔线
How to add dividers in Bottom Navigation View
我正在尝试在 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" />
感谢您的支持。
试试这个方法...
BottomNavigationView mBottomNavigationView = (BottomNavigationView) findViewById(R.id.navigation);
NavigationMenuView navMenuView = (NavigationMenuView) mBottomNavigationView.getChildAt(0);
navMenuView.addItemDecoration(new DividerItemDecoration(MainActivity.this,DividerItemDecoration.VERTICAL));
像这样创建一个 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: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" />
如果有人仍然有兴趣这样做,尽管不推荐。
您可以通过编程方式实现这一点,以避免像公认的解决方案那样在屏幕末尾出现分隔线。
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)
}
注意:点击项目时底部视图导航中的波纹效果将消失。
我正在尝试在 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" />
感谢您的支持。
试试这个方法...
BottomNavigationView mBottomNavigationView = (BottomNavigationView) findViewById(R.id.navigation);
NavigationMenuView navMenuView = (NavigationMenuView) mBottomNavigationView.getChildAt(0);
navMenuView.addItemDecoration(new DividerItemDecoration(MainActivity.this,DividerItemDecoration.VERTICAL));
像这样创建一个 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: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" />
如果有人仍然有兴趣这样做,尽管不推荐。 您可以通过编程方式实现这一点,以避免像公认的解决方案那样在屏幕末尾出现分隔线。
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)
}
注意:点击项目时底部视图导航中的波纹效果将消失。