简体   繁体   中英

Android Compose Text soft wrap without splitting the text

I am trying to load data in Text() , which looks like this

 Text( text = "15 H, 20 B, 35 C, 45 D, 50 Q, 1.2 W", textAlign = TextAlign.Center, softWrap = true, overflow = TextOverflow.Ellipsis, modifier = Modifier.padding(top = 4.dp) )

When the text is rendered, it looks like this

15 H, 20 B, 35 C, 45 D, 50 
Q, 1.2 W

I want it to not split the 50 and Q, and show them on single line, in short the value and category always to stay on same line

15 H, 20 B, 35 C, 45 D, 50 Q,
1.2 W

OR

15 H, 20 B, 35 C, 45 D, 
50 Q, 1.2 W

Try this scenario

Option 1- use \n and add that remaining text into the next line.

Text(text = "15 H, 20 B, 35 C, 45 D,\n 50 Q, 1.2 W")

Option 2- Add Max lines in Text()

  Text("15 H, 20 B, 35 C, 45 D, 50 Q, 1.2 W", maxLines = 1, overflow = TextOverflow.Ellipsis)

Option 3- Font size for Text(),but it may not gives you actual output as per screen sizes.

  Text(text = "15 H, 20 B, 35 C, 45 D, 50 Q, 1.2 W", fontSize = 12.sp,)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM