阻止文本在网址中的句点上拆分为多行
我有一个显示一些文本的 Android TextView,它是多行的。不过,在文中,我有时也会有域名;如何阻止 TextView 将行按句点分割?
例如,是否有 unicode 不间断句点?
要查看包装电子邮件地址时出现的问题,请运行android 创建项目 --target 16 --path demo --package com.example.demo --activity MainActivity
并将 res/layout/main.xml
中的文本更改为“Hello World, MyActivity 填充文本 + 电子邮件 [电子邮件受保护]
”。这会在 Galaxy S3(API 级别 16)上产生以下输出:
(根据需要调整文本以查看设备上的换行值得注意的是,在 Intellij 的布局预览中,换行是正确的,仅在设备上出现错误。)
I have an Android TextView displaying some text, and it's multi-line. However, in the text, I sometimes have domain names; how can I stop the TextView from splitting the lines up on the periods in them?
Is there a unicode non-breaking-period, for example?
To see the issue in action in wrapping an email address, runandroid create project --target 16 --path demo --package com.example.demo --activity MainActivity
and change the text in res/layout/main.xml
to "Hello World, MyActivity filler text + email [email protected]
". That produces this output on a Galaxy S3 (API level 16):
(Adjust text as appropriate to see wrapping on devices with other screen sizes. Notably, the wrapping is done correctly in Intellij's layout preview, it's only on the device that it's faulty.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
TLDR;
@Matt McMinn 已经在此处展示了此问题的解决方案,快去获取吧。我只是在这里重申该解决方案。
请注意,此问题已在 Android 4.2.2 中的平台级别得到修复。请参阅下面的屏幕截图,了解 Galaxy Nexus 上相同代码库但不同平台版本的自动换行比较。
因此,如果您不针对旧版本的 Android,则可能根本不想使用此修复程序。
代码
MainActivity.java:
activity_main.xml:
NonBreakingPeriodTextView.java:
结果
在Android 4.1.2 (Galaxy Nexus)上< /strong>
在 Android 2.3.3 上(AVD、Nexus One克隆)
希望这会有所帮助。
TLDR;
@Matt McMinn has already shown a solution for this problem here, go grab it. I am only re-iterating that solution here.
Note that, this issue has already been fixed at platform level in Android 4.2.2. See the below screenshots for word wrap comparison for the same code base but different platform versions on Galaxy Nexus.
Hence, if you are not targeting older versions of Android, you may not wish to use this fix at all.
The code
MainActivity.java:
activity_main.xml:
NonBreakingPeriodTextView.java:
The result
On Android 4.1.2 (Galaxy Nexus)
On Android 2.3.3 (AVD, Nexus One clone)
Hope this helps.
要告诉 android 解析 TextView 中的域链接,请在 TextView 代码块中使用以下代码:
这会将域名显示为应用程序中的链接,并且不会拆分行。
To tell android to parse the domain links in the TextView use this code in the TextView code block:
This will show the domain names as links in the app and wont split the lines.
在 xml 中使用:
android:singleLine="true"
Use this :
android:singleLine="true" in xml
对我来说,@ozbek 的解决方案分别是@Matt McMinn,我不得不换行,
但
除此之外还有很好的解决方案,谢谢
for me didn't work solution of @ozbek respectively @Matt McMinn, I had to change line
for
but otherwise great solution, thanks