NavigationView主题选定的项目背景样式 [英] NavigationView theme selected item background style
问题描述
我为我的应用创建了另一个主题Dark.我使用定义为
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>
通常,所选项目的文本为colorPrimary,背景似乎是某种Android灰色.当我定义新主题时,就这样使用
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>
这几乎可行,除了NavigationView中当前选中的项目外,所有内容的样式都完全符合我的要求.相反,它似乎只是colorPrimary的1个实心块(没有图标,没有文本),最后是colorControlHighlight,仅此而已.
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.
有人知道我如何在NavigationView菜单中自定义所选项目吗?我已经尝试过所有我能想到的状态,但是没有一个起作用.
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
.
示例:
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" />
在您的res/color
中
创建一个nav_state_list.xml
文件,并在其中将此放在其中:
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:
这篇关于NavigationView主题选定的项目背景样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!