如何在NavigationView中更改矢量图像的颜色

问题描述 投票:0回答:1

我需要更改菜单图标的颜色,我尝试了许多发现的方法,但是其中一种方法不起作用

 navigationView.getMenu().getItem(i).getIcon().setTint(ContextCompat.getColor(context, R.color.colorPrimary));

Drawable drawable = navigationView.getMenu().getItem(i).getIcon();
            if(drawable != null) {
                drawable.mutate();
                drawable.setColorFilter(Color.RED, PorterDuff.Mode.SRC_ATOP);
                SpannableString s = new SpannableString(name);
                s.setSpan(new ForegroundColorSpan(Current_Theme.getInt("custom_button_arrow",ContextCompat.getColor(context, R.color.custom_button_arrow))), 0, s.length(), 0);
                navigationView.getMenu().getItem(i).setTitle(s);

            }

navigationView.getMenu().getItem(i).getIcon().setColorFilter(ContextCompat.getColor(context, R.color.colorPrimary), PorterDuff.Mode.SRC_IN);

Drawable mWrappedDrawable = drawable.mutate();
                mWrappedDrawable = DrawableCompat.wrap(drawable);
                DrawableCompat.setTint(mWrappedDrawable, Color.RED);
                DrawableCompat.setTintMode(mWrappedDrawable, PorterDuff.Mode.SRC_IN);
navigationView.getMenu().getItem(i).setIcon(mWrappedDrawable);

,请帮助我

android android-studio android-menu
1个回答
0
投票

使用itemIconTint属性来更改NavigationView项目图标的颜色。

<com.google.android.material.navigation.NavigationView
            app:itemIconTint="@color/colorPrimary" />
© www.soinside.com 2019 - 2024. All rights reserved.