当前位置:网站首页>Compose text and icon splicing to realize drawableleft or drawableright
Compose text and icon splicing to realize drawableleft or drawableright
2022-07-26 07:17:00 【Ango can't move】

We want to splice this together Instead of using a Row Write two spaces
I want to use one text
Native android DrawableStart or DrawableEnd You can solve it
Compose Well
Official website Search for inlineTextContent
InlineTextContent | Android Developers
Detailed examples are as follows
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)We won't use him Go directly to our code
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(" Sign in ", "5 integral / Log in on the first day of each time ", " Have obtained 50 integral / Daily limit 100 integral ", 1f)
DailyTaskItem(" Article learning ", "10 integral / Every effective reading 1 An article ", " Obtained 50 integral / Online every day 100 integral ", 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 = " To learn ")
}
}
}
The first step is to create id Follow up Text use
The second part creates AnnotatedString Splice the original string
And Icon Represents a string of
Third parts
Build core parameters
Its width And height by Icon Size
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)
}
})
)Step 4 use
Text(text = secondayText,
inlineContent = inlineContent,
fontSize = 14.sp,
color = Color(0xff333333),
modifier = Modifier.clickable { Log.e(TAG, ":click "); }
)Design is more complex , At the same time, the system is higher .
边栏推荐
- MySQL installation tutorial - hands on installation
- It's another summer of open source. 12000 project bonuses are waiting for you!
- Now the metauniverse is no longer an existence that is only applied to the social field
- [749. Isolate virus]
- 倒计时2日!基于 Apache DolphinScheduler&TiDB 的交叉开发实践,从编写到调度让你大幅提升效率
- Become an Apache contributor, so easy!
- 每周小贴士#142:多参数构造函数和explicit
- With Huawei cloud welink, you can connect to the world even in the countryside
- Data platform scheduling upgrade and transformation | operation practice from Azkaban smooth transition to Apache dolphin scheduler
- 【QT】怎样获得QTableView和QTableWidget的行数和列数
猜你喜欢
![[arm learning (8) AXF tool analysis]](/img/6c/df2ebb3e39d1e47b8dd74cfdddbb06.gif)
[arm learning (8) AXF tool analysis]

Hands on practice - teach you how to make an intelligent fish tank with STM32

Learn browser decoding from XSS payload

Opengauss simple version installation error

Flame diagram analysis Flink backpressure

LTS(Light-Task-Scheduler)

Uncover the mystery of cloud native data management: operation level

Overview of new features of es11, ES12 and es13

Opencv learn resize and crop

No .egg-info directory found in xxx\pip-pip-egg-info-mq
随机推荐
[QT] how to obtain the number of rows and columns of qtableview and qtablewidget
如何对C盘进行扩容重新分区?
Anaconda installation tutorial - hands on installation
Now the metauniverse is no longer an existence that is only applied to the social field
Performance test -- lockust performs seckill business scenario test
Event loop in browser
Hcip - MPLS Technology
Rgb-t tracking - [dataset benchmark] gtot / rgbt210 / rgbt234 / vot-2019-2020 / laser / VTUAV
Apache DolphinScheduler&TiDB联合Meetup | 聚焦开源生态发展下的应用开发能力
NFT数字藏品系统开发:激活数字文化遗产
Opencv learning drawing shapes and text
Common programming shortcut keys of idea (take off after learning the operation)
Differences in the use of function call pointer parameters *p, * & P
Getting started with kernel PWN (5)
Unity3d-异步加载场景、进度条加载
Leetcode:1898. maximum number of removable characters [if you want to delete some IDX from a pile of things, don't use pop]
Orthogonality of 20220724 trigonometric function system
Apache Dolphinscheduler3.0.0-beta-1 版本发布,新增FlinkSQL、Zeppelin任务类型
Opencv learning warp Perspective
基于C51实现led流水灯