Android Monospace 字体不是固定宽度 [英] Android Monospace fonts aren't fixed width
问题描述
我正在为计时器编写自定义视图,但我无法为中间的数字获得正确的固定宽度字体.这是相关代码(来自两种不同的方法):
I'm writing a custom View for a timer, but I can't get a properly fixed-width font for the numbers in the middle. Here's the relevant code (from two different methods):
mTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
mTextPaint.setTypeface(Typeface.create(Typeface.MONOSPACE, Typeface.NORMAL));
// Get rectangle that is the size of the text
mTextPaint.getTextBounds(
getTimeText(),
0,
getTimeText().length(),
mTextBounds);
// X and Y coordinates of text
float mTextPosX = (width / 2) - (mTextBounds.width() / 2);
float mTextPosY = (height / 2) + (mTextBounds.height() / 2);
// Draw text
canvas.drawText(
getTimeText(),
mTextPosX,
mTextPosY,
mTextPaint);
这似乎工作正常,但是每当其中一个数字变为 1 时,该 1 占用的空间就会减少,并且整个文本块都会移动一点.这显然不应该发生,因为我使用的是等宽字体.谁能告诉我如何解决这个问题?
This seems to work fine, but whenever one of the numbers changes to a 1, that 1 takes up less space and the whole chunk of text moves over a bit. This obviously should not be happening, since I'm using a monospace font. Can anyone shed some light on how I might fix this?
推荐答案
我遇到了同样的问题.修复它的一种方法是使用外部字体,其中1"的行为符合预期.有许多字体可供尝试的网站是这个:http://www.fontcubes.com/.关于如何在 Android 中使用外部字体的相关讨论在这里:How to在 Android 中更改 TextView 的 fontFamily .
I had the same problem. A way to fix it is by using an external font in which "1" behaves as expected. A website with many fonts to try is this one: http://www.fontcubes.com/ . A related discussion on how to use external fonts in Android is here: How to change fontFamily of TextView in Android .
这篇关于Android Monospace 字体不是固定宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!