【一站式学会compose】 Android UI体系之 Text的使用和介绍

(1)Text 属性介绍:基本功能是显示一段文字

kotlin 复制代码
fun Text(
    text: String, // 文本内容
    modifier: Modifier = Modifier, // 修饰,一般背景,大小在这里设置
    color: Color = Color.Unspecified, // 颜色
    fontSize: TextUnit = TextUnit.Unspecified, // 字体大小
    fontStyle: FontStyle? = null, // 字体的变体,如斜体
    fontWeight: FontWeight? = null, // 粗细
    fontFamily: FontFamily? = null, // 字体
    letterSpacing: TextUnit = TextUnit.Unspecified, // 文字间距
    textDecoration: TextDecoration? = null, // 文本的装饰,如下划线
    textAlign: TextAlign? = null,// 文本的对其方式
    lineHeight: TextUnit = TextUnit.Unspecified, // 行间距
    overflow: TextOverflow = TextOverflow.Clip, // 文本移除的视觉效果
    softWrap: Boolean = true, // 控制文本是否能够换行
    maxLines: Int = Int.MAX_VALUE, // 最大行数
    minLines: Int = 1,// 最小行数
    onTextLayout: ((TextLayoutResult) -> Unit)? = null, //。文本花边之后的回掉
    style: TextStyle = LocalTextStyle.current // 文本样式
)

(2) 如何显示制定字符串

kotlin 复制代码
Text("hello world")
Text(stringResource(R.string.app_name))

(3)Text的style属性的使用

你可以设置的属性有:

kotlin 复制代码
        color: Color = Color.Unspecified,
        fontSize: TextUnit = TextUnit.Unspecified,
        fontWeight: FontWeight? = null,
        fontStyle: FontStyle? = null,
        fontSynthesis: FontSynthesis? = null,
        fontFamily: FontFamily? = null,
        fontFeatureSettings: String? = null,
        letterSpacing: TextUnit = TextUnit.Unspecified,
        baselineShift: BaselineShift? = null,
        textGeometricTransform: TextGeometricTransform? = null,
        localeList: LocaleList? = null,
        background: Color = Color.Unspecified,
        textDecoration: TextDecoration? = null,
        shadow: Shadow? = null,
        textAlign: TextAlign? = null,
        textDirection: TextDirection? = null,
        lineHeight: TextUnit = TextUnit.Unspecified,
        textIndent: TextIndent? = null

案例:

kotlin 复制代码
  Text(
        stringResource(R.string.app_name),
        style = TextStyle(color = Color.Blue, fontWeight = FontWeight.Bold, fontSize = 50.sp)
    )

特别注意:style参数可以在text的构造函数中直接指定,一旦指定,Text的参数会覆盖style里面的同名参数。

相关推荐
二流小码农1 天前
鸿蒙开发:上传一张参考图片便可实现页面功能
android·ios·harmonyos
鹏程十八少1 天前
4.Android 30分钟手写一个简单版shadow, 从零理解shadow插件化零反射插件化原理
android·前端·面试
Kapaseker1 天前
一杯美式搞定 Kotlin 空安全
android·kotlin
三少爷的鞋1 天前
Android 协程时代,Handler 应该退休了吗?
android
火柴就是我2 天前
让我们实现一个更好看的内部阴影按钮
android·flutter
砖厂小工2 天前
用 GLM + OpenClaw 打造你的 AI PR Review Agent — 让龙虾帮你审代码
android·github
张拭心2 天前
春节后,有些公司明确要求 AI 经验了
android·前端·人工智能
张拭心2 天前
Android 17 来了!新特性介绍与适配建议
android·前端
Kapaseker2 天前
Compose 进阶—巧用 GraphicsLayer
android·kotlin
黄林晴2 天前
Android17 为什么重写 MessageQueue
android