在Jetpack Compose中创建带边框和阴影的矩形内部文本。

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

Create text inside rectangle with border and shadow in jetpack compose

问题

我刚刚开始使用Jetpack Compose,我需要在圆角矩形边框内创建文本并添加阴影。有办法做到这一点吗?我尝试将文本放入一个框中,可以添加圆角矩形形状,但不能添加边框和阴影。它应该看起来像这样:

英文:

I've just started using the Jetpack Compose and I need to create a text inside rounded rectangle border with shadow. Is there a way to do this? I tried to put the text in a box, I can add the rounded rectangle shape, but not the border and shadow.
It should look like this:

在Jetpack Compose中创建带边框和阴影的矩形内部文本。

答案1

得分: 1

你可以这样做:

@Composable
fun TextInsideBoxScreen() {

    Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
        Box(
            contentAlignment = Alignment.Center,
            modifier = Modifier.border(width = 2.dp, color = Color.Blue, shape = CircleShape)

        ) {
            Text(
                text = "篮球",
                color = Color.LightGray,
                fontWeight = FontWeight.W800,
                modifier = Modifier.padding(horizontal = 30.dp, vertical = 10.dp)
            )
        }
    }

}
英文:

You can do like this👇

在Jetpack Compose中创建带边框和阴影的矩形内部文本。

@Composable
fun TextInsideBoxScreen() {

    Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
        Box(
            contentAlignment = Alignment.Center,
            modifier = Modifier.border(width = 2.dp, color = Color.Blue, shape = CircleShape)

        ) {
            Text(
                text = "Basketball",
                color = Color.LightGray,
                fontWeight = FontWeight.W800,
                modifier = Modifier.padding(horizontal = 30.dp, vertical = 10.dp)
            )
        }


    }

}

答案2

得分: 1

@Composable
fun RoundedCornerChip(
    content: String = "篮球",
    contentColor: Color = Color.LightGray,
    borderColor: Color = Color.Black,
) {
    val shape = RoundedCornerShape(60.dp)
    Card(
        modifier = Modifier
            .wrapContentSize().border(shape = shape, width = 2.dp, color = borderColor),
        shape = shape,
        elevation = 10.dp,
    ) {
        Box(
            contentAlignment = Alignment.Center,
            modifier = Modifier
                .wrapContentSize(),
        ) {
            Text(
                text = content,
                color = contentColor,
                fontWeight = FontWeight.W800,
                modifier = Modifier.padding(horizontal = 30.dp, vertical = 10.dp),
            )
        }
    }
}
英文:
@Composable
fun RoundedCornerChip(
    content: String = "Basket ball",
    contentColor: Color = Color.LightGray,
    borderColor: Color = Color.Black,
) {
    val shape = RoundedCornerShape(60)
    Card(
        modifier = Modifier
            .wrapContentSize().border(shape = shape, width = 2.dp, color = borderColor),
        shape = shape,
        elevation = 10.dp,
    ) {
        Box(
            contentAlignment = Alignment.Center,
            modifier = Modifier
                .wrapContentSize(),
        ) {
            Text(
                text = content,
                color = contentColor,
                fontWeight = FontWeight.W800,
                modifier = Modifier.padding(horizontal = 30.dp, vertical = 10.dp),
            )
        }
    }
}

This will give you...
在Jetpack Compose中创建带边框和阴影的矩形内部文本。

huangapple
  • 本文由 发表于 2023年3月7日 02:18:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75654432.html
匿名

发表评论

匿名网友

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

确定