【问题标题】:Why my colored icon doesn't show in navigation drawer为什么我的彩色图标没有显示在导航抽屉中
【发布时间】:2020-10-13 16:48:01
【问题描述】:

我正在制作一个 Android 应用程序。我在里面使用了侧边导航抽屉,我没有看到抽屉里的彩色图标,但是我使用了彩色图标

我的 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>

【问题讨论】:

  • 您将app:iconTint 设置为@null。您想将iconTint 设置为您希望图标的颜色。
  • 我的图标是多色的,我该怎么办?
  • 您使用哪些视图来填充NavigationView

标签: android material-components-android material-components android-navigationview


【解决方案1】:

用途:

navigationView.setItemIconTintList(null)

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

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

【讨论】:

    【解决方案2】:

    错误

    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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-12
    • 1970-01-01
    • 2018-11-12
    • 2016-12-20
    • 1970-01-01
    相关资源
    最近更新 更多