Android Jetpack Compose 图标不包含某些 material 图标

Android Jetpack Compose Icons doesn't contain some of the material icons

有很多常用的material icons in androidx.compose.material.icons.Icons but some are missing. Just as an example there is no print icon

...

import androidx.compose.material.Icon
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Menu  // ok
import androidx.compose.material.icons.filled.Print // error, unresolved reference

@Composable
fun IconsExample() {
    Icon(Icons.Filled.Menu, "menu")   // ok
    Icon(Icons.Filled.Print, "print") // error, unresolved reference
}

使用应用中丢失的图标的最简单方法是什么?

有一个单独的依赖项 material-icons-extended,其中包含 material 图标的完整列表,只需将其添加到您应用的 build.gradle

dependencies {
  ...
  implementation "androidx.compose.material:material-icons-extended:$compose_version"
}

现在您可以使用任何 material 图标,例如:

...

import androidx.compose.material.Icon
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Menu  // ok
import androidx.compose.material.icons.filled.Print // ok

@Composable
fun IconsExample() {
    Icon(Icons.Filled.Menu, "menu")   // ok
    Icon(Icons.Filled.Print, "print") // ok
}

关于工件大小的注释:由于工件包含多个主题的所有 material 图标,它是一个相当大的依赖项,截至 [=15 为 18MB aar =].有个note on maven repository建议不要直接用:

This module contains all Material icons. It is a very large dependency and should not be included directly.

考虑到大多数 Android 项目启用 code shrinking for release builds, such a large dependency won't affect the release build size but it can affect your debug build and device upload time, though I'm not sure that the influence would be significant. Actually many of compose samples 使用此依赖项。

如果只需要几个额外的图标并且您决定不使用 material-icons-extended 神器,则可以轻松地将图标添加到您的项目资源中 using Android Studio。您可以使用这样的资源图标:

...

import com.mycompany.myproject.R
import androidx.compose.ui.res.painterResource

@Composable
fun ResourceIconExample() {
    Icon(
        painter = painterResource(R.drawable.ic_baseline_print_24),
        contentDescription = "print"
    )
}