如标题所述,我想知道在单个textview元素中可以实现两个不同颜色的字符。
如标题所述,我想知道在单个textview元素中可以实现两个不同颜色的字符。
Answers:
是的,如果您格式化String
with html
的font-color
属性,然后将其传递给方法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.escapeHtml(str)
。
您可以打印多种颜色的行而无需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
文本的第一部分进行着色的示例(同时允许您动态设置颜色,而不是像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);
在此示例中,您可以将0xFFFF0000替换为 getResources().getColor(R.color.red)
我这样做是这样的:
设置颜色对文本的传递字符串和颜色:
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 name = getColoredSpanned("Hiren", "#800000");
String surName = getColoredSpanned("Patel","#000080");
在具有不同颜色的两个字符串的TextView上设置Text:
txtView.setText(Html.fromHtml(name+" "+surName));
完成了
Html.fromHtml("...")
为Html.fromHtml("...", FROM_HTML_MODE_LEGACY)
使用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;
}
最好在字符串文件中使用字符串,如下所示:
<string name="some_text">
<![CDATA[
normal color <font color=\'#06a7eb\'>special color</font>]]>
</string>
用法:
textView.text=HtmlCompat.fromHtml(getString(R.string.some_text), HtmlCompat.FROM_HTML_MODE_LEGACY)
我为其他问题写下了一些与此相似的代码,但是该问题重复了,所以我无法在那回答,所以如果有人在寻找相同的要求,我就把我的代码放在这里。
它不是完全正常工作的代码,您需要进行一些小的更改才能使其正常工作。
这是代码:
我已经使用@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);
随机颜色方法:
private int getRandomColor(){
Random rnd = new Random();
return Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
}
尽可能使用SpannableBuilder类而不是HTML格式,因为它比HTML格式解析更快。在Github上查看我自己的基准测试“ SpannableBuilder vs HTML” 谢谢!
很棒的答案!我能够使用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;
}
然后我只是setText(buildRainboxText(pack_name)); 请注意,我传递的所有单词都在15个字符以下,并且仅重复5种颜色3次-您需要根据使用情况调整数组的颜色/长度!
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;
2020年6月25日,@ canerkaseler
我想分享Kotlin答案:
fun setTextColor(tv:TextView, startPosition:Int, endPosition:Int, color:Int){
val spannableStr = SpannableString(tv.text)
val underlineSpan = UnderlineSpan()
spannableStr.setSpan(
underlineSpan,
startPosition,
endPosition,
Spanned.SPAN_INCLUSIVE_EXCLUSIVE
)
val backgroundColorSpan = ForegroundColorSpan(this.resources.getColor(R.color.agreement_color))
spannableStr.setSpan(
backgroundColorSpan,
startPosition,
endPosition,
Spanned.SPAN_INCLUSIVE_EXCLUSIVE
)
val styleSpanItalic = StyleSpan(Typeface.BOLD)
spannableStr.setSpan(
styleSpanItalic,
startPosition,
endPosition,
Spanned.SPAN_INCLUSIVE_EXCLUSIVE
)
tv.text = spannableStr
}
之后,调用上面的函数。您可以拨打多个电话:
setTextColor(textView, 0, 61, R.color.agreement_color)
setTextColor(textView, 65, 75, R.color.colorPrimary)
输出: 您可以看到下划线和彼此不同的颜色。
@canerkaseler