单个TextView与多个彩色文本

正如标题所说,我想知道是否有可能在单个文本视图元素中实现两个不同的彩色字符。

是的,如果你用htmlfont-color属性来格式化String ,然后把它传递给方法Html.fromHtml(your text here)

 String text = "<font color=#cc0029>First Color</font> <font color=#ffcc00>Second Color</font>"; yourtextview.setText(Html.fromHtml(text)); 

您可以打印具有多种颜色的线条而不使用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); 

您可以使用Spannable将效果应用于TextView

这里是我为TextView文本的第一部分着色的示例(同时允许您dynamic设置颜色,而不是像HTML示例一样将其编码为string)。

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

在这个例子中,你可以用getResources().getColor(R.color.red)代替0xFFFF0000 getResources().getColor(R.color.red)

我这样做了:

检查参考

通过传递string颜色文本设置颜色

 private String getColoredSpanned(String text, String color) { String input = "<font color=" + color + ">" + text + "</font>"; return input; } 

通过调用下面的代码在TextView / Button / EditText设置文本

TextView的:

 TextView txtView = (TextView)findViewById(R.id.txtView); 

获取彩色string:

 String name = getColoredSpanned("Hiren", "#800000"); String surName = getColoredSpanned("Patel","#000080"); 

在两个不同颜色的string的TextView上设置文本:

 txtView.setText(Html.fromHtml(name+" "+surName)); 

完成

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

嗨,我已经做到了这一点,尝试一下

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

在你的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; } 

我已经写了一些类似于这个问题的其他问题的代码,但是这个问题得到了重复,所以我不能在那里回答,所以我只是把我的代码放在这里,如果有人寻找相同的要求。

这不是完整的工作代码,你需要做一些小的改变才能实现。

这里是代码:

我使用@Graeme使用spannable文本的想法。

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

随机颜色方法:

  private int getRandomColor(){ Random rnd = new Random(); return Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)); } 

在可能的情况下使用SpannableBuilder类而不是HTML格式,因为它比HTML格式parsing更快。 在Github上查看我自己的基准testing“SpannableBuilder vs HTML”谢谢!

尝试这个:

 mBox = new TextView(context); mBox.setText(Html.fromHtml("<b>" + title + "</b>" + "<br />" + "<small>" + description + "</small>" + "<br />" + "<small>" + DateAdded + "</small>")); 

真棒的答案! 我能够使用Spannable来build立彩虹色文本(所以这可以重复任何颜色的数组)。 这是我的方法,如果它可以帮助任何人:

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

然后我只是setText(buildRainboxText(pack_name)); 请注意,我传入的所有单词都在15个字符以内,并且只重复3次5色 – 您可以根据自己的用法调整数组的颜色/长度!

 if (Build.VERSION.SDK_INT >= 24) { Html.fromHtml(String, flag) // for 24 API and more } else { Html.fromHtml(String) // or for older API } 

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

更多信息