简体   繁体   English

如何在NavigationView的xml文件中更改菜单包含项的标题颜色

[英]How to change title color of menu's enclosing item in NavigationView's xml files

I have a menu which is shown in a NavigationView (Side navigation). 我有一个菜单 ,该菜单显示在NavigationView (侧面导航)中。

The menu has this structure in which menu tags not only have their own children item tags, but they(the menu tags) are also enclosed within parent item tags. 菜单具有这种结构,其中菜单标签不仅具有自己的子标签,而且它们( 菜单标签)也包含在父标签中。

Example: 例:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:showIn="navigation_view">

    <item android:title="User Profile">
        <menu>
            <item
                android:id="@+id/profile"
                android:icon="@drawable/ic_user"
                android:title="Profile" />
            <item
                android:id="@+id/account"
                android:icon="@android:drawable/ic_secure"
                android:title="Account" />
        </menu>
    </item>

    <item android:title="Ride Now">
        <menu>
            <item
                android:id="@+id/find_ride"
                android:icon="@drawable/car_1"
                android:title="Find A Ride" />
            <item
                android:id="@+id/offer_ride"
                android:icon="@drawable/car_1"
                android:title="Offer A Ride" />
        </menu>
    </item>
    <item android:title="Booking History">
        <menu>
            <item
                android:id="@+id/offered_rides"
                android:icon="@drawable/ic_menu_share"
                android:title="Rides offered"
                />
            <item
                android:id="@+id/booked_rides"
                android:icon="@drawable/ic_menu_send"
                android:title="Rides Booked" />
            <item
                android:id="@+id/cancelled_rides"
                android:icon="@drawable/ic_menu_send"
                android:title="Rides Cancelled" />

        </menu>
    </item>

</menu>

I have been able to set the item title colors within the menu tags using: 我已经能够使用以下命令在菜单标签内设置项目标题颜色:

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:background="@color/app_dark_green_scheme"
    app:itemTextColor="@color/app_white_scheme"
    app:itemIconTint="@color/app_white_scheme"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_main"
    app:menu="@menu/activity_main_drawer" />

but I need to set the title colors for items that are parents of the menu tags. 但是我需要为菜单标签的父设置标题颜色。 How do I accomplish this? 我该如何完成?

Note: @+id/nav_view is the NavigationView that owns the menu 注意: @+id/nav_view是拥有菜单NavigationView

Please, I would prefer the answer to be xml based, instead of Java based 请,我希望答案是基于xml的,而不是基于Java的

You need add android:textColorSecondary into your style.xml 您需要将android:textColorSecondary添加到style.xml中

paste the following code into the style you are using as the theme. 将以下代码粘贴到您用作主题的样式中。 You can change the color as you like. 您可以根据需要更改颜色。

 <item name="android:textColorSecondary">@color/colorPrimary</item>

it works for me 这个对我有用

Since you don't want to change the color of all items you have to do it by code: 由于您不想更改所有项目的颜色,因此必须通过代码进行更改:

public SpannableString getColoredSpannableString(String s, int color) {
    SpannableString str = new SpannableString(s);
    str.setSpan(new ForegroundColorSpan(color), 0, s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    return str;
}

public void makeMenuItemColored(MenuItem mi, int color) {
    mi.setTitle(getColoredSpannableString(mi.getTitle().toString(), color));
}


and use it like this: 并像这样使用它:

makeMenuItemColored(mi, ContextCompat.getColor(this, R.color.colorPrimary));

or makeMenuItemColored(mi, yourColor); makeMenuItemColored(mi, yourColor);
where mi is your menu item and you can choose a color resource or a color by int 其中mi是您的菜单项,您可以通过int选择颜色资源或颜色

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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