【问题标题】:Jetpack Compose How to Align text or content of Text component with specific size to bottom left or right?Jetpack Compose 如何将具有特定大小的文本组件的文本或内容与左下角或右下角对齐?
【发布时间】:2021-02-21 14:26:58
【问题描述】:

如何使用 Jetpack Compose 将文本与 Text 组件的底部对齐? TextAlign 只有Start、End、Left、Center、Right 和Justify 选项。

  Text(
        text = "First",
        textAlign = TextAlign.Start,
        modifier = Modifier
            .background(Color(0xFF1976D2))
            .size(200.dp),
        color = Color.White,
    )

我想对齐Text 组件的内容,每个Text 都有特定的大小 使用modifier.size(x),将它们的文本对齐到底部。在图像中,蓝色矩形是Text,大小不同,应该像在经典Android 中使用android:gravity 一样对齐其中的文本。

它类似于textAlign = TextAlign.x,但用于底部。

从Box 设置对齐方式将Text 内Box 或Modifier.align(Alignment.BottomEnd) 内BoxScope 执行android:layout_gravity 对视图执行的操作,对齐文本组件,不是内容 Text 组件,你可以看到difference here。

图像中蓝色矩形的代码是

@Composable
fun BoxExample() {
    Box(
        modifier = Modifier
            .fillMaxWidth()
            .height(250.dp)
            .background(Color.LightGray)

    ) {

        // This is the one at the bottom
        Text(
            text = "First",
            modifier = Modifier
                .background(Color(0xFF1976D2))
                .size(200.dp),
            color = Color.White,
        )

        // This is the one in the middle
        Text(
            text = "Second",
            modifier = Modifier
                .background(Color(0xFF2196F3))
                .size(150.dp),
            color = Color.White
        )

        // This is the one on top
        Text(
            text = "Third ",
            modifier = Modifier
                .background(Color(0xFF64B5F6))
                .size(100.dp),
            color = Color.White
        )
    }
}

对于橙色矩形

@Composable
fun BoxShadowAndAlignmentExample() {

    Box(
        modifier = Modifier
            .fillMaxWidth()
            .height(250.dp)
            .background(Color.LightGray)
            .padding(8.dp)
    ) {

        Box(
            modifier = Modifier.shadow(
                elevation = 4.dp,
                shape = RoundedCornerShape(8.dp)
            )
        ) {
            // This is the one at the bottom
            Text(
                text = "First",
                modifier = Modifier
                    .background(Color(0xFFFFA000))
                    .size(200.dp),
                color = Color.White
            )
        }

        Box(
            modifier = Modifier.shadow(
                elevation = 4.dp,
                shape = RoundedCornerShape(8.dp)
            )
                .align(Alignment.TopEnd)

        ) {
            // This is the one in the middle
            Text(
                text = "Second",
                modifier = Modifier
                    .background(Color(0xFFFFC107))
                    .size(150.dp),
                color = Color.White
            )
        }


        val modifier = Modifier.shadow(
            elevation = 4.dp,
            shape = RoundedCornerShape(8.dp)
        )
            .align(Alignment.BottomStart)

        Box(
            modifier = modifier

        ) {
            // This is the one on top
            Text(
                text = "Third ",
                modifier = Modifier
                    .background(Color(0xFFFFD54F))

                    .size(100.dp),
                color = Color.White
            )
        }
    }
}

【问题讨论】:

    标签: android android-jetpack-compose android-jetpack-compose-text


    【解决方案1】:

    使用 align 修饰符,您可以将子组件相对于其父组件对齐在特定位置:

    Box(modifier = Modifier.fillMaxSize()) {
        Text(modifier = Modifier.align(Alignment.BottomEnd),text = "Aligned to bottom end")
        Text(modifier = Modifier.align(Alignment.BottomStart),text = "Aligned to bottom start")
        Text(modifier = Modifier.align(Alignment.CenterStart),text = "Aligned to start center ")
        Text(modifier = Modifier.align(Alignment.TopCenter),text = "Aligned to top center ")
    }
    

    【讨论】:

    • 请记住,直接从可组合文本访问 align 修饰符仅在其父级是 Box 时才有效(作为 Column 或 Row 的子级不应该正常工作)。
    • 这没有回答我的问题。它做了android:layout_gravity 对视图所做的事情。我的问题是关于如何为文本组件执行android:layout_gravity。我已经在一个盒子里对齐了橙色的文本组件。但是这些文本有一个大小,我希望它们的上下文在它们内部对齐,这是在视图类中使用 android:gravity 完成的。 Answer here explains the difference
    【解决方案2】:

    假设您的组件是一个 Box,请将您的文本放在 Box 中,如下所示:

    Box(
            modifier = Modifier.fillMaxSize(),
            Alignment.BottomStart
        ) {
            Text(
                "First",
                Modifier.padding(16.dp),
            )
        }
    

    基本上,您定义要在该组件中使用的组件部分,而不是在文本中。

    【讨论】:

    • 是的,可以使用具有特定大小的 Box 并将文本与底部对齐,但您还将为 Box 和 Text 设置背景颜色,因为 Text 会包装其内容,而 Box 必须具有例如 200dp 大小。只有Text没有更简单的方法来将它的文本对齐到任何位置吗?
    猜你喜欢
    • 2023-04-11
    • 1970-01-01
    • 1970-01-01
    • 2022-06-10
    • 2014-03-07
    • 2014-05-17
    • 1970-01-01
    • 1970-01-01
    • 2010-09-09
    相关资源
    最近更新 更多