Android 中的连字符
作为 Android 应用程序国际化的一部分,我遇到了在正确位置动态换行或连字符的需要。
我的所有字符串都外部化在 strings.xml 文件中,但我没有找到任何有关 Android 中连字符的文档。
我希望能够建议类似于 LaTeX 中的连字符位置:
http://en .wikipedia.org/wiki/Hyphenation_algorithm
但我还没有找到任何迹象表明这是否可能。我错过了框架中的任何内容吗?其他人在做什么,例如使用没有明显位置来分解句子的日语字符串?您只是在正确的位置添加空格吗?
我可以动态调整字体大小以适应某些布局,但对于跨越多行的较长消息,这是行不通的。该怎么办?
As part of internationalizing an Android application I have come across the need to dynamically word wrap or hyphenate at the right position.
All my strings are externalized in strings.xml files but I have not found any documentation about hyphenation in Android.
I would like to be able to suggest hyphenation positions similar to how I can do it in LaTeX:
http://en.wikipedia.org/wiki/Hyphenation_algorithm
But I have not found any indication if this is possible. Is there anything in the framework I have missed? What are other people doing e.g. with Japanese strings that have no obvious position to break up a sentence? Do you just add spaces at the correct positions?
I could dynamically size the font to fit into certain layouts but for longer messages that go across multiple lines that won't work. What to do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
它是 Android 6 Marshmellow 的新功能。
尝试将其添加到您的 TextView xml 中
Its a new thing from Android 6 Marshmellow.
Try adding this to your TextView xml
换行符的管理可能很麻烦。最好的选择是使用 UTF 换行修饰符控制字符,因为 android 支持完整的 UTF
我知道有人提到了“软连字符”,但还有很多。
您还可以在缺乏空格的语言中的单词之间使用“零宽度空格”,这样您就不必依赖字典解释。您还可以将其用作语言中的软连字符,允许在某些点断开行上的某些单词。
当使用您不想破坏的复合词,但希望文本转语音系统正确识别它时,您应该使用“单词分隔符”字符。不要使用“零宽度无中断空间”,因为它由于用作 BOM 而已被弃用。
最后,如果您想要一个空格但不想换行,请使用简单的不间断空格。
Management of line breaks can be a hassle. The best option is to use UTF line-break modifier control characters since android supports full UTF
I know someone mentioned "soft-hyphen", but there are quite a few more.
You can also use the "Zero Width Space" between words on languages that lack spaces so you don't have to rely on dictionary interpretation. You can also use this as a soft-hyphen in languages that allow breaking of certain words over lines at certain points.
When using a compound word that you don't want broken, but you want the Text To Speech system to recognize it properly you should use "Word Separator" character. Don't use "Zero Width Non Breaking Space" as that has been deprecated due to it's use as BOM.
Finally, if you want a space but don't want a line break, use a simple non-breaking space.
Soft hyphen 适用于从 Android 4.3 开始的 Samsung Galaxy 设备。
由于我的用例非常狭窄,我只在“verbunden”一词中使用了一个软连字符。 Unicode \u00ad 没有任何效果。
Soft hyphen worked on a Samsung Galaxy device starting with Android 4.3.
Since my use case was pretty narrow, I just used one soft hyphen in the word "verbunden". The Unicode \u00ad had no effect.
以下库支持连字符。它为您执行所有类型的文本对齐(左/右/中心/对齐)和连字符。并未添加所有语言,但可以根据需要添加。该库使用NO WEBVIEWS和SUPPORTS SPANNABLES并允许长文本。
库:https://github.com/bluejamesbond/TextJustify-Android
ANDROID:2.2 至 5.X
设置
The following library supports hyphenation. It does all types of text alignment (left/right/center/justified) and hyphenation for you. Not all languages have been added but can be added as necessary. This library uses NO WEBVIEWS and SUPPORTS SPANNABLES and allows for LONG TEXT.
LIBRARY: https://github.com/bluejamesbond/TextJustify-Android
ANDROID: 2.2 to 5.X
SETUP
这个问题很老了,但刚刚为我找到了最好的解决方案:
我不得不说我正在 Xamarin 中编程,所以代码是 C# 中的,但移植到 java 中应该不是问题。
我使用 NHyphenator 逻辑 (https://github.com/alkozko/NHyphenator) 插入 SoftHyphens( UTF8 符号 - 0x00AD),因此 Textview 在正确的位置进行连字符。
为了获得其他语言的连字符,我使用了 openoffice 词典。
例如,我必须获得德语-瑞士语言的连字符
http:// extensions.openoffice.org/en/search?f[0]=field_project_tags%3A157
1.下载扩展程序
2.用winrar或者其他东西解压
3.复制hyph_xx_xx/hyph_xx_xx.dic文件
4. 添加新的 Language 到 Hyphenator 类
NHyphernator 资源文件声明为:
hyph-xx-xx.pat.txt -->不带注释的 dic 文件内容
hyph-xx-xx.hyp.txt -->文件包含单词异常,其中连字符的逻辑不会给出正确的结果
如果有人想要 Xamarin 的便携式库,请告诉我,我可以上传它。
编辑:
在 TExtview 中,breakstrategy 应设置为 Balanced。 API级别> 23、否则不要使用breakstrategy。
编辑:
这是 mono/xamarin 代码:
https://github.com/sma84/NHyphenator-Mono
The question is old but just found best solution for me:
I have to say i'm programming in Xamarin, so the code is in C# but should not be a problem to port into java.
I used the NHyphenator Logic (https://github.com/alkozko/NHyphenator) for Inserting SoftHyphens(UTF8 Symbol - 0x00AD) so the Textview do hyphenation at the right place.
To get Hyphenation for other languages i used the openoffice Dictionaries.
e.g. i had to get Hyphenation for german-swiss language
http://extensions.openoffice.org/en/search?f[0]=field_project_tags%3A157
1. Download the extension
2. Unzip it with winrar or something else
3. Copy the hyph_xx_xx/hyph_xx_xx.dic file
4. Add new Language to the Hyphenator class
NHyphernator resource files are declared as:
hyph-xx-xx.pat.txt --> content of the dic file without comments
hyph-xx-xx.hyp.txt --> file which contains word-exceptions where the logic for hyphening does not give correct results
If anyone wants the portable Library for Xamarin just tell, i can upload it.
EDIT:
The breakstrategy should be set to Balanced in TExtview. API Level > 23, else don't use breakstrategy.
EDIT:
Here's the mono/xamarin code:
https://github.com/sma84/NHyphenator-Mono
setEllipsize 可能是如果此错误尚未修复,我们会提供帮助。
setEllipsize might be of help, if this error is yet fixed.