简体   繁体   中英

How to scale icon width by height in Jetpack Compose

I am trying to scale icon width by height. I was trying to add contentScale modifier, but it doesn't know it.

Icon(
           painter = painterResource(id = id),
           contentDescription = null,
           tint = Color.White,
           //contentScale = ContentScale.Fit,
           modifier = Modifier .fillMaxHeight(0.6f)
                               .align(CenterVertically).border(2.dp,Color.Black, RectangleShape)
    )

Picture how it looks - The icon on the left should be scaled to fit height of the black box: 左侧的图标应缩放以适合黑框的高度

Ensuring your icon is a square should be sufficient.

Modifier
    .fillMaxHeight(0.6f)
    .aspectRatio(1f) // make the width same as the height
    ... // your other modifiers

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