显示多色数字数组
Display array of numbers with multicolor
如何在对话框中用多色显示数字数组?
示例:
arr[] = {1, 2, 3, 4, 5 , 6, 7, 8, 9}
并在对话框中显示:
arr= 1 2 3 4 5 6 7 8 9
1 个红色,2 个蓝色,3 个绿色,...
我尝试在 RelativeLayout 中添加 Textview,但直到现在,我还不知道该怎么做?
Spannable 允许你添加这样的 attributes.Here 是小例子看看这个。
TextView TV = (TextView)findViewById(R.id.mytextview01);
Spannable word = new SpannableString(" 1");
word.setSpan(new ForegroundColorSpan(Color.BLUE), 0, word.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
TV.setText(word);
Spannable wordTwo = new SpannableString(" 2");
wordTwo.setSpan(new ForegroundColorSpan(Color.RED), 0, wordTwo.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
TV.append(wordTwo);
或:
您还可以使用 Html 设置颜色。
String text = "<font color=#cc0029> 1</font> <font color=#ffcc00> 2</font>";
TV.setText(Html.fromHtml(text));
您可以使用 Spannable。设置 ForegroundColorSpan 你需要定义颜色和字符间隔,例如
TextView textView = (TextView) findViewById(R.id.textview);
Spannable spannableText = new SpannableString("1 2 3");
spannableText.setSpan(new ForegroundColorSpan(Color.RED), 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannableText.setSpan(new ForegroundColorSpan(Color.GREEN), 2, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannableText.setSpan(new ForegroundColorSpan(Color.BLUE), 4, 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spannableText);
提供的解决方案可行,但在已有颜色资源类型的情况下使用字符串表示颜色是一种错误的形式。相反,请执行以下操作:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="bright_pink">#FF007F</color>
<color name="red">#FF0000</color>
<color name="orange">#FF7F00</color>
<color name="yellow">#FFFF00</color>
<color name="chartreuse">#7FFF00</color>
<color name="green">#00FF00</color>
<color name="spring_green">#00FF7F</color>
<color name="cyan">#00FFFF</color>
<color name="azure">#007FFF</color>
<color name="blue">#0000FF</color>
<color name="violet">#7F00FF</color>
<color name="magenta">#FF00FF</color>
<array name="rainbow">
<item>@color/bright_pink</item>
<item>@color/red</item>
<item>@color/orange</item>
<item>@color/yellow</item>
<item>@color/chartreuse</item>
<item>@color/green</item>
<item>@color/spring_green</item>
<item>@color/cyan</item>
<item>@color/azure</item>
<item>@color/blue</item>
<item>@color/violet</item>
<item>@color/magenta</item>
</array>
然后像这样访问它们:
int[] rainbow = context.getResources().getIntArray(R.array.rainbow);
for (int i = 0; i < tileColumns; i++) {
paint.setColor(rainbow[i]);
// Do something with the paint.
}
为此使用 StringBuilder Class:
StringBuilder sb = new StringBuilder("arr = ");
int[] arr = //this is your number array
String[] colors = // this is your color array for eg. {"#ffffff",.....}
for (int i;i<arr.length;i++) {
sb.append("<font color="+colors[i]+">"+ Integer.toString(arr[i]) +"</font>");
}
显示文本时您可以使用:
whereYouWantToDisplay.setText(Html.fromHtml(sb.toString()))
希望对您有所帮助。
如何在对话框中用多色显示数字数组?
示例:
arr[] = {1, 2, 3, 4, 5 , 6, 7, 8, 9}
并在对话框中显示:
arr= 1 2 3 4 5 6 7 8 9
1 个红色,2 个蓝色,3 个绿色,... 我尝试在 RelativeLayout 中添加 Textview,但直到现在,我还不知道该怎么做?
Spannable 允许你添加这样的 attributes.Here 是小例子看看这个。
TextView TV = (TextView)findViewById(R.id.mytextview01);
Spannable word = new SpannableString(" 1");
word.setSpan(new ForegroundColorSpan(Color.BLUE), 0, word.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
TV.setText(word);
Spannable wordTwo = new SpannableString(" 2");
wordTwo.setSpan(new ForegroundColorSpan(Color.RED), 0, wordTwo.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
TV.append(wordTwo);
或:
您还可以使用 Html 设置颜色。
String text = "<font color=#cc0029> 1</font> <font color=#ffcc00> 2</font>";
TV.setText(Html.fromHtml(text));
您可以使用 Spannable。设置 ForegroundColorSpan 你需要定义颜色和字符间隔,例如
TextView textView = (TextView) findViewById(R.id.textview);
Spannable spannableText = new SpannableString("1 2 3");
spannableText.setSpan(new ForegroundColorSpan(Color.RED), 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannableText.setSpan(new ForegroundColorSpan(Color.GREEN), 2, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannableText.setSpan(new ForegroundColorSpan(Color.BLUE), 4, 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spannableText);
提供的解决方案可行,但在已有颜色资源类型的情况下使用字符串表示颜色是一种错误的形式。相反,请执行以下操作:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="bright_pink">#FF007F</color>
<color name="red">#FF0000</color>
<color name="orange">#FF7F00</color>
<color name="yellow">#FFFF00</color>
<color name="chartreuse">#7FFF00</color>
<color name="green">#00FF00</color>
<color name="spring_green">#00FF7F</color>
<color name="cyan">#00FFFF</color>
<color name="azure">#007FFF</color>
<color name="blue">#0000FF</color>
<color name="violet">#7F00FF</color>
<color name="magenta">#FF00FF</color>
<array name="rainbow">
<item>@color/bright_pink</item>
<item>@color/red</item>
<item>@color/orange</item>
<item>@color/yellow</item>
<item>@color/chartreuse</item>
<item>@color/green</item>
<item>@color/spring_green</item>
<item>@color/cyan</item>
<item>@color/azure</item>
<item>@color/blue</item>
<item>@color/violet</item>
<item>@color/magenta</item>
</array>
然后像这样访问它们:
int[] rainbow = context.getResources().getIntArray(R.array.rainbow);
for (int i = 0; i < tileColumns; i++) {
paint.setColor(rainbow[i]);
// Do something with the paint.
}
为此使用 StringBuilder Class:
StringBuilder sb = new StringBuilder("arr = ");
int[] arr = //this is your number array
String[] colors = // this is your color array for eg. {"#ffffff",.....}
for (int i;i<arr.length;i++) {
sb.append("<font color="+colors[i]+">"+ Integer.toString(arr[i]) +"</font>");
}
显示文本时您可以使用:
whereYouWantToDisplay.setText(Html.fromHtml(sb.toString()))
希望对您有所帮助。