Compose Text 与icon拼接 实现DrawableLeft 或者DrawableRIght

 这个我们想拼接到一起 而不是用一个Row 写两个空间

想用一个text

原生android DrawableStart or DrawableEnd就可以解决

Compose呢

官网 搜索 inlineTextContent

InlineTextContent  |  Android Developers

详细实例如下

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.text.BasicText
import androidx.compose.foundation.text.InlineTextContent
import androidx.compose.foundation.text.appendInlineContent
import androidx.compose.ui.text.Placeholder
import androidx.compose.ui.text.buildAnnotatedString

val myId = "inlineContent"
val text = buildAnnotatedString {
    append("Hello")
    // Append a placeholder string "[myBox]" and attach an annotation "inlineContent" on it.
    appendInlineContent(myId, "[myBox]")
}

val inlineContent = mapOf(
    Pair(
        // This tells the [BasicText] to replace the placeholder string "[myBox]" by
        // the composable given in the [InlineTextContent] object.
        myId,
        InlineTextContent(
            // Placeholder tells text layout the expected size and vertical alignment of
            // children composable.
            Placeholder(
                width = 0.5.em,
                height = 0.5.em,
                placeholderVerticalAlign = PlaceholderVerticalAlign.AboveBaseline
            )
        ) {
            // This [Box] will fill maximum size, which is specified by the [Placeholder]
            // above. Notice the width and height in [Placeholder] are specified in TextUnit,
            // and are converted into pixel by text layout.
            Box(modifier = Modifier.fillMaxSize().background(color = Color.Red))
        }
    )
)

BasicText(text = text, inlineContent = inlineContent)

 我们就不用他的了 直接上我们的代码

package com.anguomob.compose.ui.components

import android.util.Log
import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.text.InlineTextContent
import androidx.compose.foundation.text.appendInlineContent
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.HelpOutline
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.Placeholder
import androidx.compose.ui.text.PlaceholderVerticalAlign
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp

@Composable
fun DailyTaskContent() {
    DailyTaskItem("登录", "5积分/每次首日登录", "已获得50积分/每日上限100积分", 1f)
    DailyTaskItem("文章学习", "10积分/每有效阅读1篇文章", "已获50积分/每日上线100积分", 0.7f)
}

private const val TAG = "DailyTaskContent"

@Composable
fun DailyTaskItem(title: String, secondayTitle: String, desc: String, percent: Float) {
    val inlineContentId = "inlineContentId"
    val secondayText = buildAnnotatedString {
        append(secondayTitle)
        appendInlineContent(inlineContentId, "[icon]")
    }

    val inlineContent = mapOf(
        Pair(inlineContentId,
            InlineTextContent(Placeholder(width = 14.sp,
                height = 14.sp,
                placeholderVerticalAlign = PlaceholderVerticalAlign.AboveBaseline)) { str ->
                when (str) {
                    "[icon]" -> Icon(imageVector = Icons.Default.HelpOutline,
                        contentDescription = null)
                }

            })
    )
    Row(modifier = Modifier.fillMaxWidth(),
        horizontalArrangement = Arrangement.SpaceBetween,
        verticalAlignment = Alignment.CenterVertically) {
        Column(modifier = Modifier.weight(7.5f)) {
            Text(text = title, fontSize = 16.sp, color = Color(0xff333333))

            Text(text = secondayText,
                inlineContent = inlineContent,
                fontSize = 14.sp,
                color = Color(0xff333333),
                modifier = Modifier.clickable { Log.e(TAG, ":click "); }
            )

            Row(
                verticalAlignment = Alignment.CenterVertically,
            ) {
                LinearProgressIndicator(progress = percent, modifier = Modifier.weight(1f))
                Text(text = desc,
                    fontSize = 10.sp,
                    color = Color(0xff333333),
                    modifier = Modifier
                        .weight(2f, fill = false)
                        .padding(8.dp))
            }

        }

        OutlinedButton(onClick = { },
            border = if (percent >= 1f) ButtonDefaults.outlinedBorder else BorderStroke(1.dp,
                Color(0xffff5900)),
            shape = CircleShape,
            colors = if (percent >= 1f) ButtonDefaults.outlinedButtonColors(
                contentColor = Color(0xffff5900)
            ) else {
                ButtonDefaults.outlinedButtonColors(
                    contentColor = Color(0xffff5900)
                )
            },
            modifier = Modifier
                .weight(2.5f),
            enabled = (percent < 1f)
        ) {
            Text(text = "去学习")

        }
    }
}

 第一步创建id 后续给Text用

第二部创建AnnotatedString 拼接原先字符串

与Icon代表的字符串

第三部

构建核心参数

其width 与height为 Icon的大小

    val inlineContent = mapOf(
        Pair(inlineContentId,
            InlineTextContent(Placeholder(width = 14.sp,
                height = 14.sp,
                placeholderVerticalAlign = PlaceholderVerticalAlign.AboveBaseline)) { str ->
                when (str) {
                    "[icon]" -> Icon(imageVector = Icons.Default.HelpOutline,
                        contentDescription = null)
                }

            })
    )

第四步使用

         Text(text = secondayText,
                inlineContent = inlineContent,
                fontSize = 14.sp,
                color = Color(0xff333333),
                modifier = Modifier.clickable { Log.e(TAG, ":click "); }
            )

设计起来更为复杂,也同时更为可定制度更高。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
实现跑马灯效果,可以使用`Animatable`和`AnimatableVector`来创建一个自定义的跑马灯效果的`Text`控件。 首先,需要添加以下依赖到你的项目中: ```kotlin implementation "androidx.compose.animation:animation:1.0.0" implementation "androidx.compose.animation:animation-core:1.0.0" implementation "androidx.compose.animation:animation-graphics:1.0.0" ``` 然后,可以创建一个自定义的`MarqueeText`控件,继承自`Text`: ```kotlin import androidx.compose.animation.animateColorAsState import androidx.compose.animation.core.* import androidx.compose.animation.core.AnimationConstants.Infinite import androidx.compose.foundation.layout.Box import androidx.compose.material.Text import androidx.compose.runtime.* import androidx.compose.ui.Modifier import androidx.compose.ui.draw.alpha import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.ColorFilter import androidx.compose.ui.graphics.vector.PathNode import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.unit.Dp import androidx.compose.ui.unit.dp @Composable fun MarqueeText( text: String, modifier: Modifier = Modifier, speed: Dp = 100.dp, textColor: Color = Color.Black, backgroundColor: Color = Color.White, ) { var textWidth by remember { mutableStateOf(0f) } var offsetX by remember { mutableStateOf(0f) } var isRunning by remember { mutableStateOf(false) } val infiniteTransition = rememberInfiniteTransition() val animatedValue = animateFloatAsState( targetValue = if (isRunning) -textWidth else 0f, animationSpec = infiniteRepeatable( animation = tween(durationMillis = (textWidth / speed).toInt(), easing = LinearEasing), repeatMode = RepeatMode.Restart, ) ) LaunchedEffect(text) { isRunning = true } Box(modifier = modifier) { Text( text = text, modifier = Modifier.alpha(0f), onTextLayout = { layoutResult -> textWidth = layoutResult.size.width.toFloat() } ) Box( modifier = Modifier.offset(x = animatedValue.value) .onSizeChanged { size -> offsetX = size.width.toFloat() } ) { Text( text = text, modifier = Modifier.offset(x = offsetX), color = textColor, onTextLayout = { layoutResult -> textWidth = layoutResult.size.width.toFloat() } ) } Box( modifier = Modifier.offset(x = animatedValue.value + offsetX) .onSizeChanged { size -> offsetX += size.width.toFloat() } ) { Text( text = text, color = textColor, backgroundColor = animateColorAsState(targetValue = backgroundColor).value, onTextLayout = { layoutResult -> textWidth = layoutResult.size.width.toFloat() } ) } } } ``` 然后,你可以在你的Compose函数中使用`MarqueeText`控件来实现跑马灯效果: ```kotlin @Composable fun MyScreenContent() { MarqueeText( text = "Hello, Jetpack Compose!", modifier = Modifier.fillMaxWidth(), speed = 100.dp, textColor = Color.White, backgroundColor = Color.Blue, ) } ``` 这样,你就可以在你的界面上看到一个带有跑马灯效果的文本了。你可以根据需要调整`speed`来控制跑马灯的速度,`textColor`和`backgroundColor`来设置文本和背景颜色。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

安果移不动

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值