How can I change separator color in NavigationView?

淺唱寂寞╮ 提交于 2019-11-27 00:20:12

just apply following line on style.xml

<item name="android:listDivider">your_color</item>

The below is just information for your knowledge ... If you have seen design support library .. they are using following layout for NavigationView seprator..

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_width="match_parent"
             android:layout_height="wrap_content">

    <View android:layout_width="match_parent"
          android:layout_height="1dp"
          android:background="?android:attr/listDivider"/>

</FrameLayout>

here, you can see android:background="?android:attr/listDivider" .. So enjoy ... and here is my output that i change color to holo_blue

Here is best and easy way while using menu as view

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    android:theme="@style/ThemeToolbar.NavigationView"
    app:itemTextColor="@color/white"
    app:itemIconTint="@color/white"
    app:headerLayout="@layout/activity_home_nav_header"
    app:menu="@menu/activity_home_drawer" />

ThemeToolbar.NavigatinoView

 <style name="ThemeToolbar.NavigationView" >
    <item name="android:listDivider">@color/white</item>
    <item name="android:textColorSecondary">@color/white</item>
</style>

mabc21

Create a style in your styles.xml. Enter your preferred color in the android:listDivider

<style name="NavigationView"  parent="Base.Theme.AppCompat">
    <item name="android:listDivider">@color/text_lightgray</item>
</style>

Reference the style as the theme of your navigation view:

   <android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="false"
    android:theme="@style/NavigationView"
    app:headerLayout="@layout/nav_header"
    app:menu="@menu/drawer_view"
    app:itemTextColor="@color/text_white"/>

Lastly, make sure that the groups inside your menu have unique ids. If your groups do not have the id attribute, this won't work!

<group android:checkableBehavior="single"
    android:id="@+id/group1">
    <item
        android:id="@+id/item1"
        android:title="@string/item1" />
</group>
<group android:checkableBehavior="single"
    android:id="@+id/group2">

    <item
        android:id="@+id/item2"
        android:title="@string/item2" />

</group>
RBK

yas you have to add blank header as separator.

like

<group android:id="@+id/my_id">
    <!-- Divider will appear above this item -->
    <item ... />
</group>

original answer

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:textColorPrimary">@color/menu_text_color</item>
    <item name="android:textColorSecondary">@color/menu_text_color</item>
</style>

textColorPrimary changes the color of the header.

George Freire

Perfect answer:

<android.support.design.widget.NavigationView
    android:id="@+id/activity_principal_nav_view"
    android:layout_width="@dimen/size_230"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="@color/black"
    android:fitsSystemWindows="true"
    app:insetForeground="@color/white"
    app:menu="@menu/settings_menu"
    app:itemTextColor="@color/white"
    app:itemIconTint="@color/white"
    app:headerLayout="@layout/settings_menu_header"
    android:theme="@style/NavigationDrawerStyle"/>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!