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

相关推荐
TheNextByte114 分钟前
Android上的蓝牙文件传输:跨设备无缝共享
android
言之。1 小时前
Kotlin快速入门
android·开发语言·kotlin
符哥20081 小时前
Android 权限分类说明
android
大模型玩家七七2 小时前
安全对齐不是消灭风险,而是重新分配风险
android·java·数据库·人工智能·深度学习·安全
李少兄2 小时前
MySQL 中为时间字段设置默认当前时间
android·数据库·mysql
俩个逗号。。2 小时前
修改Android resource dimens大小之后不生效
android
2501_915918412 小时前
在 iOS 环境下查看 App 详细信息与文件目录
android·ios·小程序·https·uni-app·iphone·webview
落羽的落羽2 小时前
【Linux系统】从零实现一个简易的shell!
android·java·linux·服务器·c++·人工智能·机器学习
常利兵3 小时前
Android Gradle 构建脚本现代化:Kotlin DSL (.kts) 与 Groovy DSL 深度对比与实战指南
android·开发语言·kotlin
TheNextByte13 小时前
如何在PC和Android平板之间传输文件
android·gitee·电脑