如何在Compose中创建一个显示电子邮件数字的图像。

huangapple go评论47阅读模式
英文:

How to create an image that displays email numbers on top in Compose

问题

以下是您要翻译的内容:

所以我正在尝试创建一个电子邮件显示图像,我的挑战是,在撰写邮件时,我不知道如何使电子邮件显示正确地显示在我的图像上。请查看我的代码如下。

这是我想要实现的效果。

我如何获得类似的形状。

@Composable
fun NumberedIcon(number: Int, iconRes: Int) {
Box(modifier = Modifier.size(48.dp), contentAlignment = Alignment.Center) {
Image(
painter = painterResource(id = iconRes),
contentDescription = null,
modifier = Modifier.size(32.dp),
contentScale = ContentScale.FillBounds,
alpha = 0.8f
)
Box(
modifier = Modifier
.graphicsLayer(alpha = 0.8f)
.size(24.dp)
.background(Color.White, shape = CircleShape),
contentAlignment = Alignment.Center
) {
Text(
text = number.toString(),
fontSize = 12.sp,
color = Color.Black
)
}
}
}

英文:

so I am trying to create an email display image, my challenge is, in compose I don't know how I can make the email display show correctly on my image. See my code below.

This is what I want to achieve

How can I get a similar shape.

@Composable
fun NumberedIcon(number: Int, iconRes: Int) {
    Box(modifier = Modifier.size(48.dp), contentAlignment = Alignment.Center) {
        Image(
            painter = painterResource(id = iconRes),
            contentDescription = null,
            modifier = Modifier.size(32.dp),
            contentScale = ContentScale.FillBounds,
            alpha = 0.8f
        )
        Box(
            modifier = Modifier
                .graphicsLayer(alpha = 0.8f)
                .size(24.dp)
                .background(Color.White, shape = CircleShape),
            contentAlignment = Alignment.Center
        ) {
            Text(
                text = number.toString(),
                fontSize = 12.sp,
                color = Color.Black
            )
        }
    }
}

答案1

得分: 0

Modifier.align(Alignment.TopEnd)分配给包含TextBox将其放置在父Box的顶部朝末端位置。

@Composable
fun NumberedIcon(number: Int, iconRes: Int) {
    Box(
        modifier = Modifier
            .size(48.dp),
        contentAlignment = Alignment.Center
    ) {

        Image(
            painter = painterResource(id = iconRes),
            contentDescription = null,
            modifier = Modifier
                .size(32.dp),
            contentScale = ContentScale.FillBounds,
            alpha = 0.8f
        )

        Box(
            modifier = Modifier
                .align(Alignment.TopEnd)
                .graphicsLayer(alpha = 0.8f)
                .size(24.dp)
                .background(Color.White, shape = CircleShape),
            contentAlignment = Alignment.Center
        ) {
            Text(
                text = number.toString(),
                fontSize = 12.sp,
                color = Color.Black
            )
        }
    }
}
英文:

Assigning Modifier.align(Alignment.TopEnd) to Box that contains Text will put it to top end of the parent Box

@Composable
fun NumberedIcon(number: Int, iconRes: Int) {
    Box(
        modifier = Modifier
            .size(48.dp),
        contentAlignment = Alignment.Center
    ) {

        Image(
            painter = painterResource(id = iconRes),
            contentDescription = null,
            modifier = Modifier
                .size(32.dp),
            contentScale = ContentScale.FillBounds,
            alpha = 0.8f
        )

        Box(
            modifier = Modifier
                .align(Alignment.TopEnd)
                .graphicsLayer(alpha = 0.8f)
                .size(24.dp)
                .background(Color.White, shape = CircleShape),
            contentAlignment = Alignment.Center
        ) {
            Text(
                text = number.toString(),
                fontSize = 12.sp,
                color = Color.Black
            )
        }
    }
}

huangapple
  • 本文由 发表于 2023年5月10日 20:53:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76218665.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定