安卓开发: Compose 中的 Text 文本控件属性详解

本文介绍了JetpackCompose中Text组件的使用,包括设置字体大小、颜色、粗细、斜体,单行/多行显示,字体文件引用,字间距,行间距,中划线,文字对齐,选择复制,点击监听,以及文本段落样式和多段内容的处理方法。

开发板推荐:天空星STM32F407VET6开发板

超高性价比 STM32主控 | 超高主频 | 一板兼容百芯 | 比赛神器 | 沉金彩色丝印

@Composable
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,
    onTextLayout: (TextLayoutResult) -> Unit = {},
    style: TextStyle = LocalTextStyle.current
)

 

 

 

 

字体大小、颜色、粗细、斜体样式

@Composable
fun TextTestView() {
    val content =
        "Hello Compose!"
    Column(Modifier.fillMaxSize()) {
        Text(
            text = content,
            fontSize = 20.sp, // 字体大小
            color = colorResource(id = R.color.purple_200), // 字体颜色
            fontWeight = FontWeight.Bold, // 字体加粗
            fontStyle = FontStyle.Italic // 字体斜体
        )
    }
}

单行 / 多行,超出显示 ...

在属性介绍中讲解了设置单行不允许换行 softWrap、设置最多显示几行 maxLines、内容超出截取显示 overflow,下面使用代码演示。

设置单行不允许换行,文字超出显示 ... 

开发板推荐:天空星STM32F407VET6开发板

超高性价比 STM32主控 | 超高主频 | 一板兼容百芯 | 比赛神器 | 沉金彩色丝印

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值