简体   繁体   中英

How do I make a single item in my NavigationDrawer have a different color of text?

I was able to change the color of the emergency icon to red by using this code navigationView.setItemIconTintList(null); in my Java class. I want to be able to also make the icon be red, but I can't seem to find a way to do it. Here is the picture As you can see the icon is already red but the text isn't. How do I change to word "Emergency Call" from black to Red?

Here is my Themes.xml

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.Flash" parent="Theme.MaterialComponents.DayNight.NoActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/purple_500</item>
        <item name="colorPrimaryVariant">@color/purple_700</item>
        <item name="colorOnPrimary">@color/white</item>
       
        <item name="colorSecondary">@color/teal_200</item>
        <item name="colorSecondaryVariant">@color/teal_700</item>
        <item name="colorOnSecondary">@color/black</item>
        
        <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
       

    </style>

    <style name="Theme.Flash.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

    <style name="TextAppearance44">
        <item name="android:textColor">@color/red</item>
    </style>

    <style name="Theme.Flash.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

    <style name="Theme.Flash.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>

And here is my Java class in where I have used the navigation drawer

hamburgerMenuButton = findViewById(R.id.hamburgerMenuIcon);
        hamburgerMenuButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               drawerLayout.openDrawer(GravityCompat.END);
            }
        });

        navigationView.bringToFront();
        ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(Location.this, drawerLayout,
                R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        actionBarDrawerToggle.setDrawerIndicatorEnabled(false);
        drawerLayout.addDrawerListener(actionBarDrawerToggle);
        actionBarDrawerToggle.syncState();
        navigationView.setNavigationItemSelectedListener(Location.this);
        navigationView.setItemIconTintList(null);

        MenuItem menuItem = menu.findItem(R.id.emergencyCallNavigationDrawer);
        SpannableString s = new SpannableString(menuItem.getTitle());
        s.setSpan(new TextAppearanceSpan(this, R.style.TextAppearance44), 0, s.length(), 0);
        menuItem.setTitle(s);

Update this is what happend to my code

Here is what happend to my code

You can get the menu item and then set the spannable text with the desired color to achieve this.
This is how I have achieved this:

MenuItem

Menu menu = navView.getMenu();
MenuItem menuItem = menu.findItem(id);
SpannableString s = new SpannableString(menuItem.getTitle());
s.setSpan(new TextAppearanceSpan(this, R.style.TextAppearance44), 0, s.length(), 0);
menuItem.setTitle(s);

TextAppearance44

<style name="TextAppearance44">
    <item name="android:textColor">@color/colorTextView</item>
    <item name="android:textSize">@dimen/text_size_20sp</item>
</style>

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