如何设置文本格式?

3
一个普通的记分牌文本如下所示。

enter image description here

因此,我想让文本格式看起来像这样。

enter image description here

这是代码。
    score = (TextView)findViewById(R.id.ct);
    score.setText(String.valueOf(gamescore));

                    if (gamescore > highscore){
                        high.setText(String.valueOf(gamescore));
                     }

有人可以解释一下吗?谢谢。

1
可能是Android市场文本格式的重复问题。 - Iamat8
7
请使用String.format("%06d", gamescore) - user4910279
1
@Mohit 这与那个问题根本不相似。 - Mitch Talmadge
1个回答

1
你应该使用String.format()方法和格式化语法String.format()可以用来在左侧填充字符串与零,就像你想要的一样。
这段代码将给你想要的结果:high.setText(String.format("%06d", gamescore)); 让我们详细看看发生了什么。我们正在使用此语法:%[flags][width]conversion
  • 始终使用%作为格式字符串语法的起始符号。
  • %后面是0,表示零填充标志。这告诉格式化程序结果将以零填充。
  • 在零填充标志之后是width,或者我们想要的最小位数; 在本例中,为6
  • 最后,指定转换类型。期望结果格式化为十进制整数,因此我们使用d

以下是一个示例:

int gamescore = 100;
String result = String.format("%06d", gamescore);
System.out.println(result);

输出:000100



谢谢Mitch,它可行!但是可以使用Value Animator使文本格式动画化吗? - Ricci
@Ricci,很抱歉我从未使用过ValueAnimator,所以无法帮助你。 - Mitch Talmadge

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接