正如标题所述,我想知道是否有可能在单个textview元素中实现两种不同的颜色。
正如标题所述,我想知道是否有可能在单个textview元素中实现两种不同的颜色。
是的,如果你使用 html
的 font-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.escapeHtml(str)
来转义用户输入。 - kelunikHtml.fromHtml(String)
现已被弃用,建议改为Html.fromHtml(String, Html.FROM_HTML_MODE_LEGACY)
。更多信息可以在这里找到。 - JediBurrell您可以不使用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);
java.lang.StringIndexOutOfBoundsException: length=3; index=12
的错误。 - Muhammad Babar通过传递字符串和颜色来设置文本的颜色:
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的文本:
txtView.setText(Html.fromHtml(name+" "+surName));
完成
Html.fromHtml("...")
的调用替换为对 Html.fromHtml("...", FROM_HTML_MODE_LEGACY)
的调用。 - stkentSpannable
来为TextView
应用效果: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);
getResources().getColor(R.color.red)
来替换0xFFFF0000。使用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;
}
最好使用strings文件中的字符串,例如:
<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)
我不知道从什么时候开始,这是可能的,但您可以简单地将<font> </font>
添加到您的string.xml中,这将自动更改每个文本的颜色。无需添加任何其他代码,如spannable text等。
<string name="my_formatted_text">
<font color="#FF0707">THIS IS RED</font>
<font color="#0B132B">AND NOW BLUE</font>
</string>
val greenText = SpannableString("This is green,")
greenText.setSpan(ForegroundColorSpan(resources.getColor(R.color.someGreenColor), null), 0, greenText.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
yourTextView.text = greenText
val yellowText = SpannableString("this is yellow, ")
yellowText.setSpan(ForegroundColorSpan(resources.getColor(R.color.someYellowColor), null), 0, yellowText.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
yourTextView.append(yellowText)
val redText = SpannableString("and this is red.")
redText.setSpan(ForegroundColorSpan(resources.getColor(R.color.someRedColor), null), 0, redText.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
yourTextView.append(redText)
fun TextView.append(string: String?, @ColorRes color: Int) {
if (string == null || string.isEmpty()) {
return
}
val spannable: Spannable = SpannableString(string)
spannable.setSpan(
ForegroundColorSpan(ContextCompat.getColor(context, color)),
0,
spannable.length,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
)
append(spannable)
}
现在很容易附加带有颜色的文本
textView.text = "" // Remove old text
textView.append("Red Text", R.color.colorAccent)
textView.append("White Text", android.R.color.white)