简体   繁体   English

Android jetpack compose - 可绘制颜色

[英]Android jetpack compose - Drawable colors

I am having the following issue.我有以下问题。 I am adding some vector drawable images in my project which is built using Jetpack compose.我在使用 Jetpack compose 构建的项目中添加了一些矢量可绘制图像。 I am overriding the colors of the drawable by setting我通过设置覆盖drawable的颜色

android:fillColor="?colorPrimary"

But, the previous solution, even though it works on a usual Android project, when working on Jetpack compose it is not.但是,以前的解决方案,即使它适用于通常的 Android 项目,但在 Jetpack compose 上工作时却不是。

Of course, I have initialized a Material Theme with my colors/typo/shapes.当然,我已经用我的颜色/错字/形状初始化了一个材质主题。

Any suggetions?有什么建议吗?

Best regards!此致!

I've run into similar issues where compose doesn't play well with attributes.我遇到过类似的问题,其中 compose 不能很好地处理属性。 It prefers to access via colorResource(R.color.colorName).它更喜欢通过 colorResource(R.color.colorName) 访问。

What you might be looking for is either of the below implementations:您可能正在寻找的是以下任一实现:

Icon(
 Icons.Filled, 
 "contentDescription",
 tint = MaterialTheme.colors.secondary
)

Image(
    painter = painterResource(R.drawable.resourceName), 
    contentDescription = "contentDescription", 
    colorFilter = ColorFilter.tint(Color.myColor)
)

UPDATE:更新:

I actually found something similar with font attributes.我实际上发现了与字体属性类似的东西。 You might want to try something like this:您可能想尝试这样的事情:

fun getFontFromAttribute(resId: Int, context: Context): Typeface? =
    context.obtainStyledAttributes(R.style.MYSTYLE, intArrayOf(resId))
        .use { array ->
            val resource = array.getResourceId(array.getIndex(0), 0)
            if (resource == 0) {
                null
            } else {
                ResourcesCompat.getFont(context, resource) ?: Typeface.SANS_SERIF
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM