从Jetpack Compose的IconButton中去掉黑色色调

3

我在代码中使用了 IconButton。我使用了不同颜色的可绘制对象。当我运行代码时,它显示为黑色物件。这里出了什么问题?

IconButton(
    onClick = {  }
) {
    Icon(
        painter = painterResource(R.drawable.ic_menu),
        contentDescription = null,
    )
}

实际输出

enter image description here

期望的输出

enter image description here

我在这里分享我的可绘制文件

ic_menu.xml

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="20dp"
    android:height="4dp"
    android:viewportWidth="20"
    android:viewportHeight="4">
    <path
        android:fillColor="@color/aqua"
        android:fillType="evenOdd"
        android:pathData="M2,2m-2,0a2,2 0,1 1,4 0a2,2 0,1 1,-4 0" />
    <path
        android:fillColor="@color/aqua"
        android:fillType="evenOdd"
        android:pathData="M10,2m-2,0a2,2 0,1 1,4 0a2,2 0,1 1,-4 0" />
    <path
        android:fillColor="@color/aqua"
        android:fillType="evenOdd"
        android:pathData="M18,2m-2,0a2,2 0,1 1,4 0a2,2 0,1 1,-4 0" />
</vector>

为什么不使用Icon函数的tint参数? - Thracian
需要使用色调选项吗? - Kotlin Learner
1个回答

6

如果您想使用原本的图标颜色而不是任何默认着色,您需要在 Icon 中传递 tint = Color.Unspecified

示例:

Icon(
    painter = painterResource(R.drawable.ic_menu),
    tint = Color.Unspecified,
    contentDescription = null
 )

根据官方文档,当你不提供着色(tint)时,Icon会为所有资源使用默认的黑色着色。

Icon

正如你所看到的,它使用了 LocalContentColor,这是默认为黑色的。

LocalContentColor


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接