显示多色数字数组

时间:2015-06-25 09:49:21

标签: android dialog android-custom-view

如何在对话框中多色显示数字数组?

示例:

      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但直到现在,我不知道怎么做?

4 个答案:

答案 0 :(得分:0)

Spannable允许你添加添加这样的属性。这是一个小例子看看这个。

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));

答案 1 :(得分:0)

You can use Spannable. Setting ForegroundColorSpan you need to define color and chars interval, for example

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);

答案 2 :(得分:0)

提供的解决方案可行,但如果已有颜色资源类型,则使用字符串表示颜色是不好的形式。相反,请执行以下操作:

<?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.
}

答案 3 :(得分:0)

使用StringBuilder类:

 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()))

希望这有助于。