如何在 Android 文本视图中设置字体宽度?
我正在开发一个应用程序,用于显示诸如 ascii arts 之类的信息,这些信息对字体宽度非常敏感。我一直在使用等宽字体,但它效果不佳,因为信息中存在宽字符(例如中文和日文字符),并且文本视图不会使宽字符恰好是常规字符的两倍宽。因此我想看看是否可以改变文本视图中字体的宽度,或者是否有更好的方法来解决这个问题?在我的应用程序中安装另一种等宽字体是个好主意吗?任何意见都将受到高度赞赏。谢谢。
凯文
I am developing an app that displays information such as ascii arts which are very sensitive to font width. I've been using monospace font, but it does not work well because there are wide chars (such as Chinese and Japanese characters) in the information and the textview does not make the wide chars exactly twice wide as regular chars. Therefore I am trying to see if it's possible to alter the width of fonts in a textview, or if there's better way to solve this problem? Is Installing another monospace font to my app a good idea? Any input is greatly appreciated. Thank you.
Kevin
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以尝试
通过这三种方法希望您可以将字体设置为理想的外观。
You can try
With these three methods I hope you can set the font to a desirable appearance.
对于那些想要使用 xml 设置等宽字体的人,请尝试
在添加等宽字体之前
添加 'android:typeface = "monospace"' < img src="https://i.sstatic.net/qwAd0.jpg" alt="在此处输入图像描述">
添加等宽字体后,
for those who wants to set monospace using xml, try adding 'android:typeface = "monospace"'
before adding monospace
after adding monospace
这个问题太老了,但我遇到了类似的问题,最终找到了一个很好的解决方案。
我有字体,它没有等宽字体变体。我需要在
TextView
中的几行中显示一些十六进制值,但我不想使用任何其他字体。Android 文档 说:
因此,我创建了源自
ReplacementSpan
的自定义MonospaceSpan
实现。此跨度检测给定文本的最宽字符并绘制具有相同宽度的其他字符。结果如下:
GitHub
MonospaceSpan.java
使用示例:
MainActivity.java
res/layout/activity_main.xml
This question is too old, but I got similar issue and I eventually found a good solution.
I have font, which has no monospace variant. I need to display some hexadecimal value in few lines within
TextView
, but I don't want to use any other font.Android documentation says:
So, I create custom
MonospaceSpan
implementation which derives fromReplacementSpan
. This span detects the widest char of given text and draws others with the same width.Here's result:
GitHub
MonospaceSpan.java
Example of usage:
MainActivity.java
res/layout/activity_main.xml