Are Android monospace fonts actually fixed size?

依然范特西╮ 提交于 2020-01-06 15:25:15

问题


I am using the Android Monospace font for my game and have found that the font does not behave as expected. The game relies on the top characters and the underscores below being lined up with each other as clues to solve the cryptogram. However when the word is very close to the edge of the TextView as can be seen on the 5th line shown in the image the blue line of text goes onto the next line causing the clues and answers to be out of sync.

I'm using TextView.setTypeface(Typeface.MONOSPACE) for both lines (well... a font very closely based on it - the issue happens with MONOSPACE as well though).

Any ideas what could be causing this and how to fix it?


回答1:


So it turns out that the algorithm for TextView text wrapping is NOT as simple as - does the next word fit on this line --> if not put it on the next line.

Later in that screenshot the texts in black and blue differ (one shows the author of the quote the other does not). It seems the TextViewis trying to minimise lines and after this has been accomplished it minimises the width. Given the two texts in the screenshot are ~1 line of text different the small words at the end can be rearranged to make the TextView's width smaller. This is what causes the issues - the font is (as far as I can tell) in fact perfectly monospace.

This proper solution to this problem I suppose is to extend TextView and make a custom onDraw() to deal with the problem. For me, it was sufficient to make the two texts show exactly the same words (but with one being passed through a substitution cipher).



来源:https://stackoverflow.com/questions/37971249/are-android-monospace-fonts-actually-fixed-size

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!