Altering line wrap behavior

丶灬走出姿态 提交于 2019-12-05 00:31:44

Don't know if you have found an answer to it but you can use unicode to help you out.

there is a non-break space character, so you will need to replace all the spaces you want to be unbreakable with this character (\u00A0)

for an example

String text = "Hello World";
text.replace(' ', '\u00A0');
textView.setText(text);

by the way I've looked for a span solution and couldn't find one, WrapTogetherSpan is just an interface so it wouldn't work...

but with this method I'm sure you could make a custom unbreakable span if you want.

Suragch

If you are implementing a more low-level solution (ie, drawing your own text and handling line-wrapping yourself), then see BreakIterator. The BreakIterator.getLineInstance() factory method treats email addresses as a single unit.

String text = "My email is me@example.com.";
BreakIterator boundary = BreakIterator.getLineInstance();
boundary.setText(text);
int start = boundary.first();
for (int end = boundary.next(); end != BreakIterator.DONE; end = boundary.next()) {
    System.out.println(start + " " + text.substring(start, end));
    start = end;
}

The output shows the boundary start indexes where line breaks would be acceptable.

0 My 
3 email 
9 is 
12 me@example.com.

See also

Have you tried adding android:singleLine="true" to your XML?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!