简体   繁体   English

在 Jetpack Compose 中的图标上方添加文本

[英]adding text above icon in jetpack compose

Is there any way how I can show a little description above specific icon in Jetpack Compose like in this picture?有什么办法可以像这张图片那样在 Jetpack Compose 中的特定图标上方显示一些描述吗?

在此处输入图像描述

It's called speech or tooltip bubble.它被称为语音或工具提示气泡。 You can create this or any shape using GenericShape or adding RoundedRect.您可以使用 GenericShape 或添加 RoundedRect 创建此形状或任何形状。

Column(
    modifier = Modifier
        .fillMaxSize()
        .padding(10.dp)
) {

    var showToolTip by remember {
        mutableStateOf(false)
    }


    Spacer(modifier = Modifier.height(100.dp))

    val triangleShape = remember {
        GenericShape { size: Size, layoutDirection: LayoutDirection ->
            val width = size.width
            val height = size.height

            lineTo(width / 2, height)
            lineTo(width, 0f)
            lineTo(0f, 0f)
        }
    }

    Box {

        if (showToolTip) {
            Column(modifier = Modifier.offset(y = (-48).dp)) {


                Box(
                    modifier = Modifier
                        .clip(RoundedCornerShape(10.dp))
                        .shadow(2.dp)
                        .background(Color(0xff26A69A))
                        .padding(8.dp),
                ) {
                    Text("Hello World", color = Color.White)
                }


                Box(
                    modifier = Modifier
                        .offset(x = 15.dp)
                        .clip(triangleShape)
                        .width(20.dp)
                        .height(16.dp)
                        .background(Color(0xff26A69A))
                )
            }
        }

        IconButton(
            onClick = { showToolTip = true }
        ) {
            Icon(
                imageVector = Icons.Default.Add,
                contentDescription = "null",
                Modifier
                    .background(Color.Red, CircleShape)
                    .padding(4.dp)
            )
        }
    }
}

If you need shadow or border that must be a single shape you need to build it with GenericShape .如果您需要必须是单一形状的阴影或边框,您需要使用GenericShape构建它。 You can check my answer out and library i built .您可以查看我的答案我构建的库

The sample below is simplified version of library, with no Modifier.layout which is essential for setting space reserved for arrow and setting padding correctly instead of creating another Box with Padding下面的示例是库的简化版本,没有 Modifier.layout,这对于设置为箭头保留的空间和正确设置填充而不是创建另一个带填充的 Box 是必不可少的

Result结果

在此处输入图像描述

fun getBubbleShape(
    density: Density,
    cornerRadius: Dp,
    arrowWidth: Dp,
    arrowHeight: Dp,
    arrowOffset: Dp
): GenericShape {

    val cornerRadiusPx: Float
    val arrowWidthPx: Float
    val arrowHeightPx: Float
    val arrowOffsetPx: Float

    with(density) {
        cornerRadiusPx = cornerRadius.toPx()
        arrowWidthPx = arrowWidth.toPx()
        arrowHeightPx = arrowHeight.toPx()
        arrowOffsetPx = arrowOffset.toPx()
    }

    return GenericShape { size: Size, layoutDirection: LayoutDirection ->

        val rectBottom = size.height - arrowHeightPx
        this.addRoundRect(
            RoundRect(
                rect = Rect(
                    offset = Offset.Zero,
                    size = Size(size.width, rectBottom)
                ),
                cornerRadius = CornerRadius(cornerRadiusPx, cornerRadiusPx)
            )
        )
        moveTo(arrowOffsetPx, rectBottom)
        lineTo(arrowOffsetPx + arrowWidthPx / 2, size.height)
        lineTo(arrowOffsetPx + arrowWidthPx, rectBottom)

    }
}

Then create a Bubble Composable, i set static values but you can set these as parameters然后创建一个 Bubble Composable,我设置了 static 个值,但您可以将这些设置为参数

@Composable
private fun Bubble(
    modifier: Modifier = Modifier,
    text: String
) {
    val density = LocalDensity.current
    val arrowHeight = 16.dp

    val bubbleShape = remember {
        getBubbleShape(
            density = density,
            cornerRadius = 12.dp,
            arrowWidth = 20.dp,
            arrowHeight = arrowHeight,
            arrowOffset = 30.dp
        )
    }

    Box(
        modifier = modifier
            .clip(bubbleShape)
            .shadow(2.dp)
            .background(Color(0xff26A69A))
            .padding(bottom = arrowHeight),
        contentAlignment = Alignment.Center
    ) {
        Box(modifier = Modifier.padding(8.dp)) {
            Text(
                text = text,
                color = Color.White,
                fontSize = 20.sp
            )
        }
    }
}

You can use it as in this sample.您可以像在本示例中那样使用它。 You need to change offset of Bubble to match position of ImageButton您需要更改 Bubble 的偏移量以匹配 ImageButton 的 position

Column(
    modifier = Modifier
        .fillMaxSize()
        .padding(10.dp)
) {

    var showToolTip by remember {
        mutableStateOf(false)
    }


    Spacer(modifier = Modifier.height(100.dp))

    Box {

        if (showToolTip) {
            Bubble(
                modifier = Modifier.offset(x = (-15).dp, (-52).dp),
                text = "Hello World"
            )
        }

        IconButton(
            onClick = { showToolTip = true }
        ) {
            Icon(
                imageVector = Icons.Default.Add,
                contentDescription = "null",
                Modifier
                    .background(Color.Red, CircleShape)
                    .padding(4.dp)
            )
        }
    }
}

You can use a Box.你可以使用一个盒子。 The children of the Box layout will be stacked over each other. Box 布局的子项将相互堆叠。

Box{ 
    Text(text = "Text Above Icon", modifier = text alignment)
    Icon(... , modifier = icon alignment) 
    
}

Text can be added above an icon in Jetpack Compose by using a combination of the Row and Column composables.通过结合使用行和列可组合项,可以在 Jetpack Compose 中的图标上方添加文本。 The Row composable lays out its children in a single row while the Column composable lays out its children in a single column. Row 可组合项将其子项布置在一行中,而 Column 可组合项将其子项布置在单个列中。 To add text above the icon, the Row composable should be used first, followed by the Column composable.要在图标上方添加文本,应首先使用 Row 可组合项,然后使用 Column 可组合项。 This will allow the text to be placed on the top of the icon.这将允许将文本放置在图标的顶部。 For example, the following code will add text above an icon:例如,以下代码将在图标上方添加文本:

Row { 
    Text(text = "Text Above Icon") 
    Column { 
        Icon(... ) 
    } 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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