为什么我的彩色图标没有显示在导航抽屉中

问题描述

我正在制作一个Android应用。我已经在其中使用了侧面导航抽屉,但没有看到抽屉中的颜色图标,但是我使用了颜色图标

enter image description here

我的XML代码是:

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    android:fitsSystemWindows="true"
    tools:context=".MainActivity"
    tools:openDrawer="start">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" />

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@drawable/background_pic"
        android:elevation="4dp"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/nav_header"
        app:iconTint="@null"
        app:menu="@menu/drawer_menu" />


</androidx.drawerlayout.widget.DrawerLayout>

解决方法

使用:

navigationView.setItemIconTintList(null)

或在布局中使用 app:itemIconTint 而不是app:iconTint

<com.google.android.material.navigation.NavigationView
    app:itemIconTint="@null"
,

错误

app:iconTint="@null"

使用您要使用的color名称/代码 使用它

app:itemIconTint="@null"

如果您使用的是SVG

DrawableCompat.setTint(
    DrawableCompat.wrap(myImageView.getDrawable()),ContextCompat.getColor(context,R.color.another_nice_color)
);

Change the color of drawable asset programmatically

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...