How to add separator in menu items? [duplicate]

天大地大妈咪最大 提交于 2019-12-13 01:46:19

问题


I need to add a separator between Navigation Drawer menu items.

Code for NavigationDrawer

<android.support.design.widget.NavigationView
   android:id="@+id/navigationView"
   android:layout_width="wrap_content"
   android:layout_height="match_parent"
   android:layout_gravity="start"
   android:background="@color/colorPrimary"
   android:fitsSystemWindows="true"
   android:theme="@style/NavigationTheme"
   custom:headerLayout="@layout/drawer_header"
   custom:menu="@menu/drawer_view" />

drawer_view.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
  <group android:checkableBehavior="single">
    <item
        android:id="@+id/nav_home"
        android:icon="@drawable/com_mixpanel_android_ic_bell"
        android:title="@string/home" />
    <item> </item>
    .....
    </group>
</menu>

Theme

 <style name="NavigationTheme" parent="ThemeOverlay.AppCompat.Dark">
    <item name="colorControlHighlight">@color/colorAccent</item>
</style>

r

How can I add a separator (View) between two menu_items?


回答1:


Solution is simple - Separate items into different groups (groups must have assigned different, unique id's).

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <group 
        android:id="@+id/group1"
        android:checkableBehavior="single">
    <item>(...)</item>
    </group>

    <group 
        android:id="@+id/group2"
        android:checkableBehavior="single">
    <item>(...)</item>
    </group>
</menu>


Edit:

In your situation, when you need to remove this padding, there are two solutions:

  1. Don't use custom:menu, create ListView instead with adapter and set for your ListView and add

    android:divider="#FFFFFF"
    android:dividerHeight="1px"
    
  2. This solution may not work, because parameters can be changed by Android Platform Developers:

    Add the following lines to dimens.xml

    <dimen name="design_navigation_padding_top_default" tools:override="true">0dp</dimen>
    <dimen name="design_navigation_separator_vertical_padding" tools:override="true">0dp</dimen>
    <dimen name="design_navigation_padding_bottom" tools:override="true">0dp</dimen>
    

    or

    <dimen name="navigation_separator_vertical_padding">0dp</dimen>
    


来源:https://stackoverflow.com/questions/34530355/how-to-add-separator-in-menu-items

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!