Android: avoid breaking line in TextView String parts [duplicate]

有些话、适合烂在心里 提交于 2019-12-12 09:38:25

问题


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?


回答1:


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




回答2:


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.


来源:https://stackoverflow.com/questions/18472437/android-avoid-breaking-line-in-textview-string-parts

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