简体   繁体   中英

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. I am overriding the colors of the drawable by setting

android:fillColor="?colorPrimary"

But, the previous solution, even though it works on a usual Android project, when working on Jetpack compose it is not.

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. It prefers to access via 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
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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