[英]android - Different colors in one textview
I would like to know how can I define different colors in a single textview depending on the textview repeats, for example: 我想知道如何根据重复的文本视图在单个文本视图中定义不同的颜色,例如:
If I declare something like: Color[] colors = {Color.green, Color.blue, Color.red, Color.brown, Color.yellow}; 如果我声明如下内容:Color [] colors = {Color.green,Color.blue,Color.red,Color.brown,Color.yellow};
Then I want to pass "colors" somehow in tv.setBackgroundColor(Color.GREEN); 然后我想以某种方式在tv.setBackgroundColor(Color.GREEN)中传递“颜色”; instead of "Color.green" in order that if the textview repeats five times I could see the textview in green, blue, red, brown and yellow and if it's 7 times I could get green, blue, red, brown, yellow, green and blue. 而不是“ Color.green”,以便如果文本视图重复五次,我可以看到绿色,蓝色,红色,棕色和黄色的文本视图,如果是七次,我可以看到绿色,蓝色,红色,棕色,黄色,绿色和蓝色。
Here's some code, plus an image 这是一些代码,外加一张图片
adapter = new SimpleCursorAdapter(this, R.layout.hrprocess_item_list, cursor, from, to, 0);
adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
if (view.getId() == R.id.category) {
String s=cursor.getString(cursor.getColumnIndex(manager.CONTENT_CATEGORY));
TextView tv = (TextView)view;
int[] androidColors = getResources().getIntArray(R.array.androidcolors);
for (int i = 0; i < androidColors.length; i++) {
tv.setBackgroundColor(androidColors[i]);
}
tv.setText(s);
return true;
}
return false;
}
});
lv.setAdapter(adapter);
Colors.xml Colors.xml
<item name="azul1" type="color">#4178BE</item>
<item name="verde1" type="color">#008571</item>
<item name="morado1" type="color">#9855D4</item>
<item name="rojo1" type="color">#E71D32</item>
<item name="rojo_naranja1" type="color">#D74108</item>
<item name="amarillo1" type="color">#EFC100</item>
<item name="gris1" type="color">#6D7777</item>
<integer-array name="androidcolors">
<item>@color/azul1</item>
<item>@color/verde1</item>
<item>@color/morado1</item>
<item>@color/rojo1</item>
<item>@color/rojo_naranja1</item>
<item>@color/amarillo1</item>
<item>@color/gris1</item>
</integer-array>
( http://i.stack.imgur.com/dCQep.jpg ) ( http://i.stack.imgur.com/dCQep.jpg )
Rather than using setBackgroundColor()
, you will need to apply BackgroundColorSpan
objects to the ranges that you are interested in. 而不是使用setBackgroundColor()
,您需要将BackgroundColorSpan
对象应用于您感兴趣的范围。
This sample project applies BackgroundColorSpan
to highlight search results in a TextView
: 此示例项目应用BackgroundColorSpan
突出显示TextView
搜索结果:
private void searchFor(String text) {
TextView prose=(TextView)findViewById(R.id.prose);
Spannable raw=new SpannableString(prose.getText());
BackgroundColorSpan[] spans=raw.getSpans(0,
raw.length(),
BackgroundColorSpan.class);
for (BackgroundColorSpan span : spans) {
raw.removeSpan(span);
}
int index=TextUtils.indexOf(raw, text);
while (index >= 0) {
raw.setSpan(new BackgroundColorSpan(0xFF8B008B), index, index
+ text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
index=TextUtils.indexOf(raw, text, index + text.length());
}
prose.setText(raw);
}
In your case, you would rotate through your colors, rather than using the hardcoded hex value of a color that I'm using. 在您的情况下,您可以旋转颜色,而不是使用我正在使用的颜色的硬编码十六进制值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.