简体   繁体   English

TextView中的统一文本环绕

[英]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. 它对我来说是令人满意的。
UniformTextView示例

Sources are available on github 来源可在github上找到

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 AutoFittextViewBitBucket 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: 该算法大致是:

  1. calculate String width (with Paint.measureText or something) 计算String宽度(使用Paint.measureText或其他东西)
  2. if it is less than container ( TextView ) width, just use it 如果它小于容器( TextView )宽度,只需使用它
  3. otherwise divide String width by container to know how many "\\n" to enter 否则按容器划分String宽度以知道要输入多少"\\n"
  4. with some search algorithm look for a space character the closest to 1/nth of the width (again using measureText if the font is not monospace) 使用一些搜索算法查找最接近宽度的1 / n的空格字符(如果字体不是等宽measureText则再次使用measureText
  5. substring at that point and repeat point 4 for n-1 (n > 1) 在该点处的子串并且对于n-1(n> 1)重复点4

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.

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