2re*_*d13 320
是的,如果格式化Stringwith 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));
Run Code Online (Sandbox Code Playgroud)
Swa*_*wal 160
您可以在不使用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);
Run Code Online (Sandbox Code Playgroud)
Gra*_*eme 33
您可以使用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);
Run Code Online (Sandbox Code Playgroud)
在此示例中,您可以使用a替换0xFFFF0000 getResources().getColor(R.color.red)
Hir*_*tel 32
我这样做了:
设置颜色对文本的传递字符串和颜色:
private String getColoredSpanned(String text, String color) {
String input = "<font color=" + color + ">" + text + "</font>";
return input;
}
Run Code Online (Sandbox Code Playgroud)
通过调用以下代码在TextView/Button/EditText等上设置文本:
TextView的:
TextView txtView = (TextView)findViewById(R.id.txtView);
Run Code Online (Sandbox Code Playgroud)
获取彩色字符串:
String name = getColoredSpanned("Hiren", "#800000");
String surName = getColoredSpanned("Patel","#000080");
Run Code Online (Sandbox Code Playgroud)
在TextView上设置两个不同颜色的字符串:
txtView.setText(Html.fromHtml(name+" "+surName));
Run Code Online (Sandbox Code Playgroud)
完成
Bis*_*kar 28
使用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);
Run Code Online (Sandbox Code Playgroud)
嘿伙计们,我已经做到了,试试吧
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)));
Run Code Online (Sandbox Code Playgroud)
在你的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;
}
Run Code Online (Sandbox Code Playgroud)
@Swapnil Kotwal 答案的 Kotlin 版本。
Android Studio 4.0.1、科特林 1.3.72
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)
Run Code Online (Sandbox Code Playgroud)
我不知道,因为什么时候这是可能的,但您可以简单地添加<font> </font>到 string.xml 中,它将自动更改每个文本的颜色。无需添加任何额外的代码,例如可扩展文本等。
<string name="my_formatted_text">
<font color="#FF0707">THIS IS RED</font>
<font color="#0B132B">AND NOW BLUE</font>
</string>
Run Code Online (Sandbox Code Playgroud)
我已经写下了与此类似的其他问题的一些代码,但是该问题重复了,所以我无法回答那里,所以如果有人寻找相同的要求,我只是将我的代码放在这里。
它不是完全工作的代码,您需要进行一些小的更改才能使其工作。
这是代码:
我使用了@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);
Run Code Online (Sandbox Code Playgroud)
随机颜色方法:
private int getRandomColor(){
Random rnd = new Random();
return Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
}
Run Code Online (Sandbox Code Playgroud)
小智 6
使用 Kotlin 和扩展,您可以非常简单、干净地添加彩色文本:
使用此内容创建文件TextViewExtensions.kt
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)
}
Run Code Online (Sandbox Code Playgroud)
现在很容易添加带有颜色的文本
textView.text = "" // Remove old text
textView.append("Red Text", R.color.colorAccent)
textView.append("White Text", android.R.color.white)
Run Code Online (Sandbox Code Playgroud)
基本上与@Abdul Rizwan答案相同,但使用 Kotlin、扩展、一些验证并在扩展内获取颜色。
最好使用字符串文件中的字符串,例如:
<string name="some_text">
<![CDATA[
normal color <font color=\'#06a7eb\'>special color</font>]]>
</string>
Run Code Online (Sandbox Code Playgroud)
用法:
textView.text=HtmlCompat.fromHtml(getString(R.string.some_text), HtmlCompat.FROM_HTML_MODE_LEGACY)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
108188 次 |
| 最近记录: |