简体   繁体   中英

NavigationView theme selected item background style

I have created an alternative theme for my app which is Dark. I use a navigation bar which is defined as

<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"
    app:headerLayout="@layout/nav_header_main"
    app:menu="@menu/activity_main_drawer" />

It has two groups defined within the menu which are selectable

<item
    android:id="@+id/sort_item"
    android:title="One">
    <menu>
        <group
            android:id="@+id/menu_group_sort"
            android:checkableBehavior="single">
            <item
                android:id="@+id/nav_sort_new"
                android:title="@string/menu_new" />
            <item
                android:id="@+id/nav_sort_hot"
                android:title="@string/menu_hot" />
            <item
                android:id="@+id/nav_sort_top"
                android:title="@string/menu_top" />
        </group>
    </menu>
</item>

<item
    android:id="@+id/filter_item"
    android:title="Two">
    <menu>
        <group
            android:id="@+id/menu_group_filter"
            android:checkableBehavior="single">
            <item
                android:id="@+id/nav_category_all"
                android:title="@string/menu_all" />
            <item
                android:id="@+id/nav_category_business"
                android:title="@string/menu_business" />
            <item
                android:id="@+id/nav_category_technology"
                android:title="@string/menu_technology" />
            <item
                android:id="@+id/nav_category_politics"
                android:title="@string/menu_politics" />
        </group>
    </menu>
</item>

Normally, the selected item's text is colorPrimary, and the background appears to be some sort of Android grey. When I define my new theme, I use it as such

<!-- Dark theme. -->
<style name="AppTheme.Dark" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/colorPrimaryInverse</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDarkInverse</item>
    <item name="colorAccent">@color/colorAccentInverse</item>
    <item name="android:textColor">@color/textColorPrimaryInverse</item>
    <item name="android:textColorPrimary">@color/textColorPrimaryInverse</item>
    <item name="android:textColorSecondary">@color/textColorSecondaryInverse</item>
    <item name="android:textColorPrimaryInverse">@color/textColorPrimary</item>
    <item name="android:textColorSecondaryInverse">@color/textColorSecondary</item>
    <item name="android:background">@color/colorPrimaryInverse</item>

    <item name="colorControlHighlight">@color/colorAccentInverse</item>
</style>

This almost works, everything is styled exactly how I want it except the currently selected item in the NavigationView. Instead, it appears to just be 1 solid block of colorPrimary (no icon, no text), and on the ends is the colorControlHighlight but that's it.

Does anyone know how I can customize a selected item within a NavigationView menu? I've tried every state I could think of, but none worked.

You don't have to customize the style for this one, you can set a Color State List to the NavigationView app:itemTextColor .


Example:

NavigationView :

<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"
    app:itemTextColor="@color/nav_state_list"
    app:headerLayout="@layout/nav_header_drawer"
    app:menu="@menu/activity_drawer_drawer" />

Create a nav_state_list.xml file in your res/color and put this inside:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Item checked -->
    <item android:color="#313189" android:state_checked="true" />
    <!-- Item pressed -->
    <item android:color="#318931" android:state_pressed="true" />
    <!-- Item default -->
    <item android:color="#893131" />
</selector>

This is how it will look after: 在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM