[英]Uniform text wrapping in TextView
I need nice text wrapping in TextView, especially for text in headers. 我需要在TextView中使用漂亮的文本包装,尤其是标题中的文本。
Text wrapping for TextView might look like this, where the last word is in new line: TextView的文本换行可能如下所示,最后一个单词在新行中:
| ========================= |
| ===== |
That what I would like to have is wrapping where lines width is more equable: 我想要的是包装宽度更均匀的地方:
| ================ |
| ============== |
It's easy to add '\\n' for one language and test it on different screen sizes but not when there is more than 10 translations. 可以很容易地为一种语言添加'\\ n'并在不同的屏幕尺寸上测试它,但在有超过10种翻译时则不能。
I have modified TextView and created UniformTextView. 我修改了TextView并创建了UniformTextView。 After investigating of TextView sources I have decided to minimize TextView's width to have preferred lines number.
在调查TextView源代码后,我决定将TextView的宽度最小化,以获得首选的行号。
<pl.dziobas.uniformtextview.UniformTextView
android:text="@string/sample_text"
android:layout_height="wrap_content"
android:layout_width="match_parent"
app:prefLineNumber="2"
style="@style/sample_style" />
It works satisfactorily for me. 它对我来说是令人满意的。
You can add a '\\n' in your string resouce xml to add a newline so you can managethe wrapping yourself. 您可以在字符串资源xml中添加'\\ n'来添加换行符,以便您可以自行管理换行。
Another approach would be to dynamically add the '\\n' where you get the string length divided by 2 and search for the next space in either direction and on the first find you just add '\\n' there. 另一种方法是动态添加'\\ n',你得到字符串长度除以2并在任一方向搜索下一个空格,在第一个找到你只需添加'\\ n'。 A hack, but probably work.
一个黑客,但可能工作。
Other than that there is not much in Android for Hyphenation or Typography. 除此之外,在Android中没有太多用于连字符或排版的内容。 Propably this post will give you some tips: http://smarter-than-the-average-pierre.blogspot.co.at/2011/10/bad-android-typography-tale-of-text.html
这篇文章可能会给你一些提示: http : //smarter-than-the-average-pierre.blogspot.co.at/2011/10/bad-android-typography-tale-of-text.html
There is an open source project in Github AutoFittextView and in BitBucket AutoScaletextView . Github AutoFittextView和BitBucket AutoScaletextView中有一个开源项目。
You can change according to your requirement. 您可以根据您的要求进行更改。
I tried the AutoScaleTextview and reached to the below OutPut . 我尝试了AutoScaleTextview并到达了下面的OutPut 。
Hope this will help you. 希望这会帮助你。
The algorithm would be roughly: 该算法大致是:
String
width (with Paint.measureText
or something) String
宽度(使用Paint.measureText
或其他东西) TextView
) width, just use it TextView
)宽度,只需使用它 String
width by container to know how many "\\n"
to enter String
宽度以知道要输入多少"\\n"
measureText
if the font is not monospace) measureText
则再次使用measureText
) Probably will require some adjustments like using container width by some small percent smaller than real. 可能需要进行一些调整,比如使用容器宽度比实际小一些小。
If you know whats the text in advance (I means if its a hard coded text), you can fix the width of TextView in dp, so that it wraps up properly, or else you may also use android:ems to fix the maximum number of characters in a line. 如果您事先知道文本是什么(我的意思是它是硬编码文本),您可以在dp中修复TextView的宽度,以便它正确包装,否则您也可以使用android:ems来修复最大数量一行中的字符。 So your TextView can be:
所以你的TextView可以是:
<TextView
android:layout_height="wrap_content"
android:layout_width="100dp"
android:lines="2"/>
OR 要么
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:ems="15"
android:lines="2" />
I hope this helps! 我希望这有帮助!
Check out Android's own autoTextSizing for TextView
, added in add API 26 with Support Library that supports from API 14 onwards with app:autoSizeTextType="uniform"
. 查看Android自己的TextTextSizing for
TextView
,添加API 26和支持库,支持从API 14开始支持app:autoSizeTextType="uniform"
。
Have fun! 玩得开心!
https://developer.android.com/guide/topics/ui/look-and-feel/autosizing-textview.html https://developer.android.com/guide/topics/ui/look-and-feel/autosizing-textview.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.