NavigationView theme selected item background style

北城以北 提交于 2019-12-07 21:55:20

问题


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.


回答1:


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:



来源:https://stackoverflow.com/questions/35160827/navigationview-theme-selected-item-background-style

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