• Compose中的Text组件


    纵向布局为:Column

    横向布局为:Row

    设置内容

    text属性设置内容。

    设置权重

    Modifier.weight(1.0f, true)权重设置,第一个参数 Float类型设置占比权重,第二个参数当为true时,元素将占据分配的整个宽度。

    fontSize设置字体大小,FontFamily(Font(R.font.shizizuo))设置字体样式。

    设置字体间距

    letterSpacing 设置字体间距。

    1. Text(
    2. //权重设置
    3. modifier = Modifier.weight(1.0f, true),
    4. text = "当前数量:$index",
    5. fontSize = 30.sp,
    6. //字体样式设置
    7. fontFamily = FontFamily(Font(R.font.shizizuo)),
    8. //字体颜色设置
    9. color = colorResource(id = R.color.purple_500),
    10. //设置字符间距
    11. letterSpacing = 5.sp
    12. )

    更多属性参考源码:

    1. fun Text(
    2. text: String,
    3. modifier: Modifier = Modifier,
    4. color: Color = Color.Unspecified,
    5. fontSize: TextUnit = TextUnit.Unspecified,
    6. fontStyle: FontStyle? = null,
    7. fontWeight: FontWeight? = null,
    8. fontFamily: FontFamily? = null,
    9. letterSpacing: TextUnit = TextUnit.Unspecified,
    10. textDecoration: TextDecoration? = null,
    11. textAlign: TextAlign? = null,
    12. lineHeight: TextUnit = TextUnit.Unspecified,
    13. overflow: TextOverflow = TextOverflow.Clip,
    14. softWrap: Boolean = true,
    15. maxLines: Int = Int.MAX_VALUE,
    16. onTextLayout: (TextLayoutResult) -> Unit = {},
    17. style: TextStyle = LocalTextStyle.current
    18. )

    设置下划线与删除线

    TextDecoration.Underline给字体设置下划线,TextDecoration.LineThrough给字体设置删除线,extAlign.Center设置字体内容居中。

    1. Text(
    2. //宽度全屏
    3. modifier = Modifier.fillMaxWidth(),
    4. text = "Hello world!",
    5. //下划线
    6. textDecoration = TextDecoration.Underline,
    7. //内容居中
    8. textAlign = TextAlign.Center
    9. )
    10. Text(
    11. modifier = Modifier.fillMaxWidth(),
    12. text = "Hello world!",
    13. //中划(删除/过时)线
    14. textDecoration = TextDecoration.LineThrough,
    15. //内容居中
    16. textAlign = TextAlign.Center
    17. )

    设置行高

    lineHeight设置行高,fontSize设置字体大小,maxLines设置最大行,TextOverflow.Clip设置文本超出不显示,TextOverflow.Ellipsis设置超出文本省略号显示。

    1. Text(
    2. "素胚勾勒出青花笔锋浓转淡",
    3. //行高
    4. lineHeight = 35.sp,
    5. //字符间距
    6. letterSpacing = 35.sp,
    7. //字体大小
    8. fontSize = 15.sp,
    9. //最大行
    10. // maxLines = 1,
    11. //剪切溢出的文本以固定其容器。
    12. // overflow = TextOverflow.Clip,
    13. //使用省略号表示文本已溢出。
    14. // overflow = TextOverflow.Ellipsis,
    15. )

    设置指定字符样式

    使用buildAnnotatedString属性设置内容,SpanStyle设置字符样式。

    1. Text(
    2. modifier = Modifier.fillMaxWidth(),
    3. text = buildAnnotatedString {
    4. withStyle(
    5. style = SpanStyle(
    6. fontWeight = FontWeight.Bold,
    7. color = Color.Blue,
    8. fontSize = 20.sp
    9. )
    10. ) {
    11. append("H")
    12. }
    13. append("ello\t")
    14. withStyle(
    15. style = SpanStyle(
    16. fontWeight = FontWeight.Bold,
    17. color = Color.Green,
    18. fontSize = 20.sp
    19. )
    20. ) {
    21. append("W")
    22. }
    23. append("orld")
    24. },
    25. textAlign = TextAlign.Center
    26. )

     长按复制与过滤复制

    SelectionContainer属性中添加的内容支持长按复制。SelectionContainer中设置DisableSelection属性,DisableSelection中设置的内容可跳过复制。

    1. SelectionContainer(modifier = Modifier.fillMaxWidth()) {
    2. Column {
    3. Text(
    4. "天青色等烟雨",
    5. modifier = Modifier.fillMaxWidth(),
    6. textAlign = TextAlign.Center
    7. )
    8. Text(
    9. "而我在等你",
    10. modifier = Modifier.fillMaxWidth(),
    11. textAlign = TextAlign.Center
    12. )
    13. Text(
    14. "月色被打捞起",
    15. modifier = Modifier.fillMaxWidth(),
    16. textAlign = TextAlign.Center
    17. )
    18. //过滤复制
    19. DisableSelection {
    20. Text(
    21. "晕开了结局",
    22. modifier = Modifier.fillMaxWidth(),
    23. textAlign = TextAlign.Center
    24. )
    25. Text(
    26. "如传世的青花瓷",
    27. modifier = Modifier.fillMaxWidth(),
    28. textAlign = TextAlign.Center
    29. )
    30. }
    31. Text(
    32. "自顾自美丽",
    33. modifier = Modifier.fillMaxWidth(),
    34. textAlign = TextAlign.Center
    35. )
    36. Text(
    37. "你眼带笑意",
    38. modifier = Modifier.fillMaxWidth(),
    39. textAlign = TextAlign.Center
    40. )
    41. }
    42. }

    完整代码:

    1. import android.os.Bundle
    2. import androidx.activity.ComponentActivity
    3. import androidx.activity.compose.setContent
    4. import androidx.compose.foundation.BorderStroke
    5. import androidx.compose.foundation.layout.Column
    6. import androidx.compose.foundation.layout.Row
    7. import androidx.compose.foundation.layout.fillMaxSize
    8. import androidx.compose.foundation.layout.fillMaxWidth
    9. import androidx.compose.foundation.layout.padding
    10. import androidx.compose.foundation.shape.RoundedCornerShape
    11. import androidx.compose.foundation.text.selection.DisableSelection
    12. import androidx.compose.foundation.text.selection.SelectionContainer
    13. import androidx.compose.material3.Button
    14. import androidx.compose.material3.ButtonDefaults
    15. import androidx.compose.material3.MaterialTheme
    16. import androidx.compose.material3.Surface
    17. import androidx.compose.material3.Text
    18. import androidx.compose.runtime.Composable
    19. import androidx.compose.runtime.mutableStateOf
    20. import androidx.compose.runtime.saveable.rememberSaveable
    21. import androidx.compose.ui.Alignment
    22. import androidx.compose.ui.Modifier
    23. import androidx.compose.ui.graphics.Color
    24. import androidx.compose.ui.res.colorResource
    25. import androidx.compose.ui.res.stringResource
    26. import androidx.compose.ui.text.SpanStyle
    27. import androidx.compose.ui.text.buildAnnotatedString
    28. import androidx.compose.ui.text.font.Font
    29. import androidx.compose.ui.text.font.FontFamily
    30. import androidx.compose.ui.text.font.FontWeight
    31. import androidx.compose.ui.text.style.TextAlign
    32. import androidx.compose.ui.text.style.TextDecoration
    33. import androidx.compose.ui.text.withStyle
    34. import androidx.compose.ui.tooling.preview.Preview
    35. import androidx.compose.ui.unit.dp
    36. import androidx.compose.ui.unit.sp
    37. import com.cwj.composedemo.ui.theme.ComposeDemoTheme
    38. class MainActivity : ComponentActivity() {
    39. override fun onCreate(savedInstanceState: Bundle?) {
    40. super.onCreate(savedInstanceState)
    41. setContent {
    42. ComposeDemoTheme {
    43. // A surface container using the 'background' color from the theme
    44. Surface(
    45. modifier = Modifier.fillMaxSize(),
    46. color = MaterialTheme.colorScheme.background
    47. ) {
    48. //动态改变内容,初始值设置为0
    49. val index = rememberSaveable { mutableStateOf(0) }
    50. Greeting(index.value) {
    51. index.value = it
    52. }
    53. }
    54. }
    55. }
    56. }
    57. }
    58. @Composable
    59. fun Greeting(index: Int, onIndexChangeInt: (Int) -> Unit) {
    60. //纵向布局
    61. Column(
    62. modifier = Modifier.fillMaxSize(),
    63. //纵向居中
    64. // verticalArrangement = Arrangement.Center,
    65. //横向居中
    66. // horizontalAlignment = Alignment.CenterHorizontally,
    67. ) {
    68. //横向布局
    69. Row(
    70. Modifier.padding(10.dp, 10.dp),
    71. //纵向居中
    72. verticalAlignment = Alignment.CenterVertically
    73. ) {
    74. Text(
    75. //权重设置
    76. modifier = Modifier.weight(1.0f, true),
    77. text = "当前数量:$index",
    78. fontSize = 30.sp,
    79. //字体样式设置
    80. fontFamily = FontFamily(Font(R.font.shizizuo)),
    81. //字体颜色设置
    82. color = colorResource(id = R.color.purple_500),
    83. //设置字符间距
    84. letterSpacing = 5.sp
    85. )
    86. Button(
    87. //边框宽度与颜色设置
    88. border = BorderStroke(2.dp, color = colorResource(id = R.color.purple_200)),
    89. //圆角设置
    90. shape = RoundedCornerShape(8.dp),
    91. colors = ButtonDefaults.buttonColors(
    92. //容器颜色
    93. containerColor = colorResource(id = R.color.purple_500),
    94. //内容颜色
    95. contentColor = colorResource(id = R.color.white),
    96. ),
    97. onClick = {
    98. onIndexChangeInt(index + 1)
    99. }
    100. ) {
    101. Text(
    102. text = stringResource(id = R.string.add),
    103. //斜体
    104. // fontStyle = FontStyle.Italic,
    105. //加粗
    106. // fontWeight = FontWeight.Bold,
    107. )
    108. }
    109. }
    110. Text(
    111. //宽度全屏
    112. modifier = Modifier.fillMaxWidth(),
    113. text = "Hello world!",
    114. //下划线
    115. textDecoration = TextDecoration.Underline,
    116. //内容居中
    117. textAlign = TextAlign.Center
    118. )
    119. Text(
    120. modifier = Modifier.fillMaxWidth(),
    121. text = "Hello world!",
    122. //中划(删除/过时)线
    123. textDecoration = TextDecoration.LineThrough,
    124. //内容居中
    125. textAlign = TextAlign.Center
    126. )
    127. Text(
    128. "素胚勾勒出青花笔锋浓转淡",
    129. //行高
    130. lineHeight = 35.sp,
    131. //字符间距
    132. letterSpacing = 35.sp,
    133. //字体大小
    134. fontSize = 15.sp,
    135. //最大行
    136. // maxLines = 1,
    137. //剪切溢出的文本以固定其容器。
    138. // overflow = TextOverflow.Clip,
    139. //使用省略号表示文本已溢出。
    140. // overflow = TextOverflow.Ellipsis,
    141. )
    142. Text(
    143. modifier = Modifier.fillMaxWidth(),
    144. text = buildAnnotatedString {
    145. withStyle(
    146. style = SpanStyle(
    147. fontWeight = FontWeight.Bold,
    148. color = Color.Blue,
    149. fontSize = 20.sp
    150. )
    151. ) {
    152. append("H")
    153. }
    154. append("ello\t")
    155. withStyle(
    156. style = SpanStyle(
    157. fontWeight = FontWeight.Bold,
    158. color = Color.Green,
    159. fontSize = 20.sp
    160. )
    161. ) {
    162. append("W")
    163. }
    164. append("orld")
    165. },
    166. textAlign = TextAlign.Center
    167. )
    168. //长按复制
    169. SelectionContainer(modifier = Modifier.fillMaxWidth()) {
    170. Column {
    171. Text(
    172. "天青色等烟雨",
    173. modifier = Modifier.fillMaxWidth(),
    174. textAlign = TextAlign.Center
    175. )
    176. Text(
    177. "而我在等你",
    178. modifier = Modifier.fillMaxWidth(),
    179. textAlign = TextAlign.Center
    180. )
    181. Text(
    182. "月色被打捞起",
    183. modifier = Modifier.fillMaxWidth(),
    184. textAlign = TextAlign.Center
    185. )
    186. //过滤复制
    187. DisableSelection {
    188. Text(
    189. "晕开了结局",
    190. modifier = Modifier.fillMaxWidth(),
    191. textAlign = TextAlign.Center
    192. )
    193. Text(
    194. "如传世的青花瓷",
    195. modifier = Modifier.fillMaxWidth(),
    196. textAlign = TextAlign.Center
    197. )
    198. }
    199. Text(
    200. "自顾自美丽",
    201. modifier = Modifier.fillMaxWidth(),
    202. textAlign = TextAlign.Center
    203. )
    204. Text(
    205. "你眼带笑意",
    206. modifier = Modifier.fillMaxWidth(),
    207. textAlign = TextAlign.Center
    208. )
    209. }
    210. }
    211. }
    212. }
    213. @Preview(showBackground = true, showSystemUi = true)
    214. @Composable
    215. fun GreetingPreview() {
    216. ComposeDemoTheme {
    217. //动态改变内容,初始值设置为0
    218. val index = rememberSaveable { mutableStateOf(0) }
    219. Greeting(index.value) {
    220. index.value = it
    221. }
    222. }
    223. }

  • 相关阅读:
    网络渗透课2
    VMware虚拟机中的Linux通过NAT模式共享主机网卡实现与外部设备通信
    Chrome扩展开发实战:网页图片抓取,打造专属自己的效率插件
    大话STL第六期——map/multimap
    一小时上手微信小程序开发
    跨平台游戏引擎 Axmol-2.0.0 正式发布
    rocky linux 8.5 基本设置
    Nginx gateway集群和动态网关
    AndroidStudio 新建工程的基本修改及事件添加
    计算机的发展史以及未来 App和Cloud这10年发展很快,被忽略的硬性的螺旋上升 QCon 大会2022
  • 原文地址:https://blog.csdn.net/juer2017/article/details/133633302