如何在NavigationView中更改分隔符颜色?

时间:2022-11-20 21:24:10

I'm trying to use NavigationView to implement NavigationDrawer. I have added the separator by setting group id in menu. However I can't see the separator. I guess it is because the separator color is same as the background. So I want to change the separator color. But I find no way to change it. Can anyone help me?

我正在尝试使用NavigationView来实现NavigationDrawer。我通过在菜单中设置组ID添加了分隔符。但是我看不到分隔符。我想这是因为分隔符颜色与背景相同。所以我想更改分隔符颜色。但我发现无法改变它。谁能帮我?

In screenshot, it seems have a room for separator between History and Settings, but you can't see it.

在屏幕截图中,它似乎在历史记录和设置之间有分隔空间,但您无法看到它。

如何在NavigationView中更改分隔符颜色?

activity_main.xml

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:fitsSystemWindows="true"
    tools:context=".ui.MapActivity">

    <LinearLayout
        android:id="@+id/main_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <include layout="@layout/toolbar_actionbar" />

        <!--main content-->
    </LinearLayout>

    <android.support.design.widget.NavigationView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/navdrawer_background"
        app:insetForeground="#4000"
        app:itemTextColor="@color/navdrawer_item_text_color"
        app:itemIconTint="@color/navdrawer_item_icon_tint"
        app:headerLayout="@layout/drawer_header"
        app:menu="@menu/drawer"/>
</android.support.v4.widget.DrawerLayout>

drawer.xml

drawer.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <group android:id="@+id/group_feature"
        android:checkableBehavior="single">
        <item android:id="@+id/navdrawer_item_map"
            android:checked="true"
            android:icon="@drawable/ic_drawer_map"
            android:title="@string/navdrawer_item_map"/>
        <item android:id="@+id/navdrawer_item_favourite"
            android:icon="@drawable/ic_drawer_fav"
            android:title="@string/navdrawer_item_fav"/>
        <item android:id="@+id/navdrawer_item_history"
            android:icon="@drawable/ic_drawer_history"
            android:title="@string/navdrawer_item_history"/>
    </group>
    <group android:id="@+id/group_settings"
        android:checkableBehavior="single">
        <item android:id="@+id/navdrawer_item_settings"
            android:icon="@drawable/ic_drawer_settings"
            android:title="@string/navdrawer_item_settings"/>
        <item android:id="@+id/navdrawer_item_help"
            android:icon="@drawable/ic_drawer_help"
            android:title="@string/navdrawer_item_help"/>
        <item android:id="@+id/navdrawer_item_about"
            android:icon="@drawable/ic_drawer_about"
            android:title="@string/navdrawer_item_about"/>
    </group>
</menu>

6 个解决方案

#1


80  

just apply following line on style.xml

只需在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..

以下是您的知识信息...如果您已经看过设计支持库..他们使用以下布局为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 如何在NavigationView中更改分隔符颜色?

在这里,你可以看到android:background =“?android:attr / listDivider”..所以享受...这里是我的输出,我改变颜色为holo_blue

#2


27  

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

ThemeToolbar.NavigatinoView

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

如何在NavigationView中更改分隔符颜色?

#3


1  

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

原始答案

#4


1  

<!-- 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.

textColorPrimary更改标题的颜色。

#5


1  

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

在styles.xml中创建一个样式。在android:listDivider中输入您的首选颜色

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

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!

最后,确保菜单中的组具有唯一ID。如果您的组没有id属性,这将不起作用!

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

#6


-1  

Perfect answer:

完美答案:

如何在NavigationView中更改分隔符颜色?

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

#1


80  

just apply following line on style.xml

只需在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..

以下是您的知识信息...如果您已经看过设计支持库..他们使用以下布局为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 如何在NavigationView中更改分隔符颜色?

在这里,你可以看到android:background =“?android:attr / listDivider”..所以享受...这里是我的输出,我改变颜色为holo_blue

#2


27  

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

ThemeToolbar.NavigatinoView

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

如何在NavigationView中更改分隔符颜色?

#3


1  

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

原始答案

#4


1  

<!-- 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.

textColorPrimary更改标题的颜色。

#5


1  

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

在styles.xml中创建一个样式。在android:listDivider中输入您的首选颜色

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

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!

最后,确保菜单中的组具有唯一ID。如果您的组没有id属性,这将不起作用!

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

#6


-1  

Perfect answer:

完美答案:

如何在NavigationView中更改分隔符颜色?

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