为什么String.format()无法在Android TextView上运行?

And*_*res 3 java android textview

我正在尝试在Android TextView上格式化文本.这是我在Java控制台中尝试的代码:

System.out.println(String.format("%-7s 56", "S"));
System.out.println(String.format("%-7s 56", "A(102)"));
Run Code Online (Sandbox Code Playgroud)

输出是预期的,我的文字左对齐:

S       56
A(102)  56
Run Code Online (Sandbox Code Playgroud)

不,我在Android TextView上尝试相同的代码:

mTextView.setText(String.format("%-7s 56\n", "S"));
mTextView.append(String.format("%-7s 56", "A(102)"));
Run Code Online (Sandbox Code Playgroud)

这就是结果:

在此输入图像描述

如您所见,输出不是预期的文本未对齐.我做错了什么?有没有其他方法可以做到这一点?

And*_*res 8

问题是我没有使用Monospaced字体.

等宽字体,也称为固定间距,固定宽度或非比例字体,是字体和字符各自占据相同数量的水平空间的字体

所以我在写入TextView之前只添加了这一行:

mTextView.setTypeface(Typeface.MONOSPACE);
Run Code Online (Sandbox Code Playgroud)