Android: avoid breaking line in TextView String pa

2019-04-26 15:26发布

This question already has an answer here:

I would like to avoid breaking lines in specifical parts of the String.

Let's say we have this String:

Speed (m/s)

The ideal would be either not jumping at all and having the full String in one line, or, if the jump is needed, in this way:

Speed
(m/s)

What I want to avoid are thing like this:

Speed (m/
s)

Any ideas?

2条回答
2楼-- · 2019-04-26 15:51

Try to use non-breaking space between the Speed and "(m/s)". The Unicode no-break space character (\u00A0) should work for TextView.

查看更多
淡お忘
3楼-- · 2019-04-26 15:51

Try this (just an idea, not tested):

  1. Calculate the width of the TextView with View.getMeasuredWidth().
  2. Calculate the width of the String with Paint.measureText().
  3. Compare the widths and add "\n" between "Speed" and "(m/s)" if required.
查看更多
登录 后发表回答