【一站式学会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里面的同名参数。

相关推荐
plainGeekDev33 分钟前
工厂模式 → Hilt
android·java·kotlin
雨白40 分钟前
深入理解 Kotlin 协程 (十):引而不发,探秘 Flow 冷流机制与异常透明性
android·kotlin
plainGeekDev1 小时前
Application 单例 → Hilt Singleton
android·java·kotlin
程序员码歌2 小时前
我全程用 AI开发了一款微信小游戏,上线了
android·前端·游戏开发
黄林晴3 小时前
你敢信吗?同样是跨端,KMP 跟 RN 居然差这么多!
android·前端
zbmwa4 小时前
jetpack compose 副作用 produceState
android
hunterandroid5 小时前
Android 后台任务可靠性排查:从 WorkManager 观测到失败重试闭环
android·前端
GitLqr7 小时前
Flutter FocusNode 实战指南:玩转键盘焦点与用户输入体验
android·flutter·ios
zzq77978 小时前
Android 17 升级后 System.load 报错排查与加固兼容指南
android·安全·app加固·apk加固·御盾安全·免费加固·御盾加固
酷在前行8 小时前
【R绘图】Nature Communications 半眼图复刻:分布、区间与多面板排版(保姆级教程)
android·开发语言·r语言