[英]how to set text color randomly in android?
Random myColor = new Random();
tv.setTextColor(Color.rgb(myColor.nextInt(255), myColor.nextInt(255), myColor.nextInt(255)));
string.xml:
<TextView
android:id="@+id/score"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Score"
android:textColor="@color/yellow"
/>
这将循环,我希望每个score
文本具有不同的颜色。 但是它不起作用
您必须获取0到255之间的数字,因此请生成一种方法来获取此数字以清理代码:
private int getN() {
return (int) Math.random() * 255;
}
并将随机颜色设置为tv
...
tv.setTextColor(Color.rgb(getN(), getN(), getN()));
使用随机数 :
Random rand = new Random();
Color color = new Color(rand.nextFloat(), rand.nextFloat(), rand.nextFloat());
然后:
tv.setTextColor(color);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.