As the title says, I want to know is it possible to achieve two different colored characters in a single textview element.
正如标题所说,我想知道是否可以在一个textview元素中实现两个不同颜色的字符。
11 个解决方案
#1
286
yes, if you format the String
with html
's font-color
property then pass it to the method Html.fromHtml(your text here)
是的,如果用html的字体颜色属性格式化字符串,然后将其传递给方法html . fromhtml(这里的文本)
String text = "<font color=#cc0029>First Color</font> <font color=#ffcc00>Second Color</font>";
yourtextview.setText(Html.fromHtml(text));
#2
135
You can prints lines with multiple colors without HTML as:
你可以打印多种颜色的线条而不用HTML作为:
TextView textView = (TextView) findViewById(R.id.mytextview01);
Spannable word = new SpannableString("Your message");
word.setSpan(new ForegroundColorSpan(Color.BLUE), 0, word.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(word);
Spannable wordTwo = new SpannableString("Your new message");
wordTwo.setSpan(new ForegroundColorSpan(Color.RED), 0, wordTwo.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.append(wordTwo);
#3
28
You can use Spannable
to apply effects to your TextView
:
你可以使用Spannable应用效果到你的TextView:
Here is my example for colouring just the first part of a TextView
text (while allowing you to set the color dynamically rather than hard coding it into a String as with the HTML example!)
这里是我为TextView文本的第一部分上色的示例(同时允许您动态地设置颜色,而不是像HTML示例那样硬编码成字符串!)
mTextView.setText("Red text is here", BufferType.SPANNABLE);
Spannable span = (Spannable) mTextView.getText();
span.setSpan(new ForegroundColorSpan(0xFFFF0000), 0, "Red".length(),
Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
In this example you can replace 0xFFFF0000 with a getResources().getColor(R.color.red)
在本例中,可以用getResources().getColor(R.color.red)替换0xff0000
#4
26
I have done this way:
我这样做了:
Set Color on Text by passing String and color:
通过传递字符串和颜色在文本上设置颜色:
private String getColoredSpanned(String text, String color) {
String input = "<font color=" + color + ">" + text + "</font>";
return input;
}
Set text on TextView / Button / EditText etc by calling below code:
通过调用下面的代码在TextView / Button / EditText等上设置文本:
TextView:
TextView:
TextView txtView = (TextView)findViewById(R.id.txtView);
Get Colored String:
得到颜色的字符串:
String name = getColoredSpanned("Hiren", "#800000");
String surName = getColoredSpanned("Patel","#000080");
Set Text on TextView of two strings with different colors:
在两个不同颜色的字符串的TextView上设置文本:
txtView.setText(Html.fromHtml(name+" "+surName));
Done
完成
#5
17
Use SpannableStringBuilder
使用SpannableStringBuilder
SpannableStringBuilder builder = new SpannableStringBuilder();
SpannableString str1= new SpannableString("Text1");
str1.setSpan(new ForegroundColorSpan(Color.RED), 0, str1.length(), 0);
builder.append(str1);
SpannableString str2= new SpannableString(appMode.toString());
str2.setSpan(new ForegroundColorSpan(Color.GREEN), 0, str2.length(), 0);
builder.append(str2);
TextView tv = (TextView) view.findViewById(android.R.id.text1);
tv.setText( builder, TextView.BufferType.SPANNABLE);
#6
5
Hey guys I have done this, try it
嘿,伙计们,我做过了,试试
TextView textView=(TextView)findViewById(R.id.yourTextView);//init
//here I am appending two string into my textView with two diff colors.
//I have done from fragment so I used here getActivity(),
//If you are trying it from Activity then pass className.this or this;
textView.append(TextViewUtils.getColoredString(getString(R.string.preString),ContextCompat.getColor(getActivity(),R.color.firstColor)));
textView.append(TextViewUtils.getColoredString(getString(R.string.postString),ContextCompat.getColor(getActivity(),R.color.secondColor)));
Inside you TextViewUtils class add this method
在TextViewUtils类中添加这个方法。
/***
*
* @param mString this will setup to your textView
* @param colorId text will fill with this color.
* @return string with color, it will append to textView.
*/
public static Spannable getColoredString(String mString, int colorId) {
Spannable spannable = new SpannableString(mString);
spannable.setSpan(new ForegroundColorSpan(colorId), 0, spannable.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Log.d(TAG,spannable.toString());
return spannable;
}
#7
4
I have write down some code for other question which is similar to this one, but that question got duplicated so i can't answer there so i am just putting my code here if someone looking for same requirement.
我已经写了一些类似于这个问题的代码,但是这个问题被重复了,所以我不能回答,所以我只是把我的代码放在这里,如果有人在寻找同样的要求。
It's not fully working code, you need to make some minor changes to get it worked.
它不是完全工作的代码,您需要做一些小的修改来使它工作。
Here is the code:
这是代码:
I've used @Graeme idea of using spannable text.
我使用了@Graeme的概念来使用可扩展文本。
String colorfulText = "colorfulText";
Spannable span = new SpannableString(colorfulText);
for ( int i = 0, len = colorfulText.length(); i < len; i++ ){
span.setSpan(new ForegroundColorSpan(getRandomColor()), i, i+1,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
((TextView)findViewById(R.id.txtSplashscreenCopywrite)).setText(span);
Random Color Method:
随机颜色的方法:
private int getRandomColor(){
Random rnd = new Random();
return Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
}
#8
2
Use SpannableBuilder class instead of HTML formatting where it possible because it more faster then HTML format parsing. See my own benchmark "SpannableBuilder vs HTML" on Github Thanks!
尽可能使用SpannableBuilder类而不是HTML格式,因为它比HTML格式解析更快。在Github上看到我自己的基准“SpannableBuilder vs HTML”,谢谢!
#9
1
Try this:
试试这个:
mBox = new TextView(context);
mBox.setText(Html.fromHtml("<b>" + title + "</b>" + "<br />" +
"<small>" + description + "</small>" + "<br />" +
"<small>" + DateAdded + "</small>"));
#10
0
Awesome answers! I was able to use Spannable to build rainbow colored text (so this could be repeated for any array of colors). Here's my method, if it helps anyone:
很棒的答案!我可以使用Spannable来构建彩虹色的文本(因此,对于任何颜色的数组,都可以重复此操作)。这是我的方法,如果它对任何人都有帮助的话:
private Spannable buildRainbowText(String pack_name) {
int[] colors = new int[]{Color.RED, 0xFFFF9933, Color.YELLOW, Color.GREEN, Color.BLUE, Color.RED, 0xFFFF9933, Color.YELLOW, Color.GREEN, Color.BLUE, Color.RED, 0xFFFF9933, Color.YELLOW, Color.GREEN, Color.BLUE, Color.RED, 0xFFFF9933, Color.YELLOW, Color.GREEN, Color.BLUE};
Spannable word = new SpannableString(pack_name);
for(int i = 0; i < word.length(); i++) {
word.setSpan(new ForegroundColorSpan(colors[i]), i, i+1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
return word;
}
And then I just setText(buildRainboxText(pack_name)); Note that all of the words I pass in are under 15 characters and this just repeats 5 colors 3 times - you'd want to adjust the colors/length of the array for your usage!
然后我设置ext(buildRainboxText(pack_name));注意,我传入的所有单词都在15个字符以下,这只会重复5种颜色3次——您需要调整数组的颜色/长度以供您使用!
#11
0
if (Build.VERSION.SDK_INT >= 24) {
Html.fromHtml(String, flag) // for 24 API and more
} else {
Html.fromHtml(String) // or for older API
}
for 24 API and more (flag)
对于24个API和更多(标志)
public static final int FROM_HTML_MODE_COMPACT = 63;
public static final int FROM_HTML_MODE_LEGACY = 0;
public static final int FROM_HTML_OPTION_USE_CSS_COLORS = 256;
public static final int FROM_HTML_SEPARATOR_LINE_BREAK_BLOCKQUOTE = 32;
public static final int FROM_HTML_SEPARATOR_LINE_BREAK_DIV = 16;
public static final int FROM_HTML_SEPARATOR_LINE_BREAK_HEADING = 2;
public static final int FROM_HTML_SEPARATOR_LINE_BREAK_LIST = 8;
public static final int FROM_HTML_SEPARATOR_LINE_BREAK_LIST_ITEM = 4;
public static final int FROM_HTML_SEPARATOR_LINE_BREAK_PARAGRAPH = 1;
public static final int TO_HTML_PARAGRAPH_LINES_CONSECUTIVE = 0;
public static final int TO_HTML_PARAGRAPH_LINES_INDIVIDUAL = 1;
更多信息
#1
286
yes, if you format the String
with html
's font-color
property then pass it to the method Html.fromHtml(your text here)
是的,如果用html的字体颜色属性格式化字符串,然后将其传递给方法html . fromhtml(这里的文本)
String text = "<font color=#cc0029>First Color</font> <font color=#ffcc00>Second Color</font>";
yourtextview.setText(Html.fromHtml(text));
#2
135
You can prints lines with multiple colors without HTML as:
你可以打印多种颜色的线条而不用HTML作为:
TextView textView = (TextView) findViewById(R.id.mytextview01);
Spannable word = new SpannableString("Your message");
word.setSpan(new ForegroundColorSpan(Color.BLUE), 0, word.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(word);
Spannable wordTwo = new SpannableString("Your new message");
wordTwo.setSpan(new ForegroundColorSpan(Color.RED), 0, wordTwo.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.append(wordTwo);
#3
28
You can use Spannable
to apply effects to your TextView
:
你可以使用Spannable应用效果到你的TextView:
Here is my example for colouring just the first part of a TextView
text (while allowing you to set the color dynamically rather than hard coding it into a String as with the HTML example!)
这里是我为TextView文本的第一部分上色的示例(同时允许您动态地设置颜色,而不是像HTML示例那样硬编码成字符串!)
mTextView.setText("Red text is here", BufferType.SPANNABLE);
Spannable span = (Spannable) mTextView.getText();
span.setSpan(new ForegroundColorSpan(0xFFFF0000), 0, "Red".length(),
Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
In this example you can replace 0xFFFF0000 with a getResources().getColor(R.color.red)
在本例中,可以用getResources().getColor(R.color.red)替换0xff0000
#4
26
I have done this way:
我这样做了:
Set Color on Text by passing String and color:
通过传递字符串和颜色在文本上设置颜色:
private String getColoredSpanned(String text, String color) {
String input = "<font color=" + color + ">" + text + "</font>";
return input;
}
Set text on TextView / Button / EditText etc by calling below code:
通过调用下面的代码在TextView / Button / EditText等上设置文本:
TextView:
TextView:
TextView txtView = (TextView)findViewById(R.id.txtView);
Get Colored String:
得到颜色的字符串:
String name = getColoredSpanned("Hiren", "#800000");
String surName = getColoredSpanned("Patel","#000080");
Set Text on TextView of two strings with different colors:
在两个不同颜色的字符串的TextView上设置文本:
txtView.setText(Html.fromHtml(name+" "+surName));
Done
完成
#5
17
Use SpannableStringBuilder
使用SpannableStringBuilder
SpannableStringBuilder builder = new SpannableStringBuilder();
SpannableString str1= new SpannableString("Text1");
str1.setSpan(new ForegroundColorSpan(Color.RED), 0, str1.length(), 0);
builder.append(str1);
SpannableString str2= new SpannableString(appMode.toString());
str2.setSpan(new ForegroundColorSpan(Color.GREEN), 0, str2.length(), 0);
builder.append(str2);
TextView tv = (TextView) view.findViewById(android.R.id.text1);
tv.setText( builder, TextView.BufferType.SPANNABLE);
#6
5
Hey guys I have done this, try it
嘿,伙计们,我做过了,试试
TextView textView=(TextView)findViewById(R.id.yourTextView);//init
//here I am appending two string into my textView with two diff colors.
//I have done from fragment so I used here getActivity(),
//If you are trying it from Activity then pass className.this or this;
textView.append(TextViewUtils.getColoredString(getString(R.string.preString),ContextCompat.getColor(getActivity(),R.color.firstColor)));
textView.append(TextViewUtils.getColoredString(getString(R.string.postString),ContextCompat.getColor(getActivity(),R.color.secondColor)));
Inside you TextViewUtils class add this method
在TextViewUtils类中添加这个方法。
/***
*
* @param mString this will setup to your textView
* @param colorId text will fill with this color.
* @return string with color, it will append to textView.
*/
public static Spannable getColoredString(String mString, int colorId) {
Spannable spannable = new SpannableString(mString);
spannable.setSpan(new ForegroundColorSpan(colorId), 0, spannable.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Log.d(TAG,spannable.toString());
return spannable;
}
#7
4
I have write down some code for other question which is similar to this one, but that question got duplicated so i can't answer there so i am just putting my code here if someone looking for same requirement.
我已经写了一些类似于这个问题的代码,但是这个问题被重复了,所以我不能回答,所以我只是把我的代码放在这里,如果有人在寻找同样的要求。
It's not fully working code, you need to make some minor changes to get it worked.
它不是完全工作的代码,您需要做一些小的修改来使它工作。
Here is the code:
这是代码:
I've used @Graeme idea of using spannable text.
我使用了@Graeme的概念来使用可扩展文本。
String colorfulText = "colorfulText";
Spannable span = new SpannableString(colorfulText);
for ( int i = 0, len = colorfulText.length(); i < len; i++ ){
span.setSpan(new ForegroundColorSpan(getRandomColor()), i, i+1,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
((TextView)findViewById(R.id.txtSplashscreenCopywrite)).setText(span);
Random Color Method:
随机颜色的方法:
private int getRandomColor(){
Random rnd = new Random();
return Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
}
#8
2
Use SpannableBuilder class instead of HTML formatting where it possible because it more faster then HTML format parsing. See my own benchmark "SpannableBuilder vs HTML" on Github Thanks!
尽可能使用SpannableBuilder类而不是HTML格式,因为它比HTML格式解析更快。在Github上看到我自己的基准“SpannableBuilder vs HTML”,谢谢!
#9
1
Try this:
试试这个:
mBox = new TextView(context);
mBox.setText(Html.fromHtml("<b>" + title + "</b>" + "<br />" +
"<small>" + description + "</small>" + "<br />" +
"<small>" + DateAdded + "</small>"));
#10
0
Awesome answers! I was able to use Spannable to build rainbow colored text (so this could be repeated for any array of colors). Here's my method, if it helps anyone:
很棒的答案!我可以使用Spannable来构建彩虹色的文本(因此,对于任何颜色的数组,都可以重复此操作)。这是我的方法,如果它对任何人都有帮助的话:
private Spannable buildRainbowText(String pack_name) {
int[] colors = new int[]{Color.RED, 0xFFFF9933, Color.YELLOW, Color.GREEN, Color.BLUE, Color.RED, 0xFFFF9933, Color.YELLOW, Color.GREEN, Color.BLUE, Color.RED, 0xFFFF9933, Color.YELLOW, Color.GREEN, Color.BLUE, Color.RED, 0xFFFF9933, Color.YELLOW, Color.GREEN, Color.BLUE};
Spannable word = new SpannableString(pack_name);
for(int i = 0; i < word.length(); i++) {
word.setSpan(new ForegroundColorSpan(colors[i]), i, i+1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
return word;
}
And then I just setText(buildRainboxText(pack_name)); Note that all of the words I pass in are under 15 characters and this just repeats 5 colors 3 times - you'd want to adjust the colors/length of the array for your usage!
然后我设置ext(buildRainboxText(pack_name));注意,我传入的所有单词都在15个字符以下,这只会重复5种颜色3次——您需要调整数组的颜色/长度以供您使用!
#11
0
if (Build.VERSION.SDK_INT >= 24) {
Html.fromHtml(String, flag) // for 24 API and more
} else {
Html.fromHtml(String) // or for older API
}
for 24 API and more (flag)
对于24个API和更多(标志)
public static final int FROM_HTML_MODE_COMPACT = 63;
public static final int FROM_HTML_MODE_LEGACY = 0;
public static final int FROM_HTML_OPTION_USE_CSS_COLORS = 256;
public static final int FROM_HTML_SEPARATOR_LINE_BREAK_BLOCKQUOTE = 32;
public static final int FROM_HTML_SEPARATOR_LINE_BREAK_DIV = 16;
public static final int FROM_HTML_SEPARATOR_LINE_BREAK_HEADING = 2;
public static final int FROM_HTML_SEPARATOR_LINE_BREAK_LIST = 8;
public static final int FROM_HTML_SEPARATOR_LINE_BREAK_LIST_ITEM = 4;
public static final int FROM_HTML_SEPARATOR_LINE_BREAK_PARAGRAPH = 1;
public static final int TO_HTML_PARAGRAPH_LINES_CONSECUTIVE = 0;
public static final int TO_HTML_PARAGRAPH_LINES_INDIVIDUAL = 1;
更多信息