How to change color of text and icon of Sub-Menu attached to Navigation view?

て烟熏妆下的殇ゞ 提交于 2019-12-05 13:44:29

Your submenu needs to be wrapped in a menu & group tag like below. This will allow you to select one of any of the menu items at a time. You can select them by setting the item as checked in your NavigationView.OnNavigationItemSelectedListener.

<?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/nav1"
            android:checked="true"
            android:icon="@drawable/myd1"
            android:title="Nav 1"
            />
    </group>
    <item android:title="@string/nav_item_subheading_app">
        <menu>
            <group android:checkableBehavior="single">
                <item
                    android:id="@+id/nav1"
                    android:icon="@drawable/myd1"
                    android:title="Nav 1"
                    />
            </group>
        </menu>
    </item>
</menu>

Changing the color of the header and Subheader in the Navigation View and paste the below code in your res>values>styles.xml

 <style name="NavigationViewStyle">
    <item name="android:textSize">16sp</item> <!-- menu item text size-->
    <item name="android:listPreferredItemHeightSmall">40dp</item><!-- menu item height-->
    <item name="android:textColorPrimary">@android:color/white</item>
    <item name="android:textColorSecondary">#FFB300</item>
</style>

NavigationView set the header color as textcolor secondary and subheader color as the textColor Primary .

finally, add this style in the navigationView

 <android.support.design.widget.NavigationView
    android:id="@+id/navview"
    android:layout_width="300dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="#022F56"
    app:headerLayout="@layout/sidebar_header"
    app:theme="@style/NavigationViewStyle"
    app:menu="@menu/sidebar_home"/>

And the output will be,

Thats all, happy Coding.

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