[英]Is it possible to change the text color in a string to multiple colors in Java?
我的意思是,是否可以在单个字符串中将文本“This text is blue”更改为蓝色? 一定有办法...
<TextView
android:gravity="left"
android:padding="3dip"
android:text="This text is white. This text is blue."
android:textColor="#ffffff"
android:textSize="22dp"/>
是的,这是可能的。 为此,您需要使用SpannableString
和ForegroundColorSpan
。
这应该是这样的:
SpannableStringBuilder builder = new SpannableStringBuilder();
String red = "this is red";
SpannableString redSpannable= new SpannableString(red);
redSpannable.setSpan(new ForegroundColorSpan(Color.RED), 0, red.length(), 0);
builder.append(redSpannable);
String white = "this is white";
SpannableString whiteSpannable= new SpannableString(white);
whiteSpannable.setSpan(new ForegroundColorSpan(Color.WHITE), 0, white.length(), 0);
builder.append(whiteSpannable);
String blue = "this is blue";
SpannableString blueSpannable = new SpannableString(blue);
blueSpannable.setSpan(new ForegroundColorSpan(Color.BLUE), 0, blue.length(), 0);
builder.append(blueSpannable);
mTextView.setText(builder, BufferType.SPANNABLE);
一种简单的方法是使用 HTML 并以编程方式将文本设置为TextView
。
String text = "This text is white. <font color=\"blue\">This text is blue.</font>";
textView.setText(Html.fromHtml(text), BufferType.SPANNABLE);
尝试这个..
TextView update= (TextView) dialog.findViewById(R.id.address);
String colorText= "Driver is nearby "
+ "<font color=\"#E72A02\"><bold>"
+ "43, KR Rd, Tata Silk Farm, Jayanagar"
+ "</bold></font>"
+ " and he is "
+ "<font color=\"#B92000\"><bold>"
+ "11 km"
+ "</bold></font>"
+ " & "
+ "<font color=\"#B92000\"><bold>"
+ "20 mins"
+ "</bold></font>"
+ " away from your current location.";
update.setText(Html.fromHtml(colorText));
结果会是这样..
Html.fromHtml(String)
在 Android N 中已弃用为了支持最新版本的 Android 所以像这样
val colorText = ("Some Normal Text\n"
+ "<font color=\"#FFA500\"><bold> Orange Text </bold></font>"
+ "More Normal text")
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
venueAddressValue.setText(Html.fromHtml(colorText, Html.FROM_HTML_MODE_LEGACY));
} else {
venueAddressValue.setText(Html.fromHtml(colorText));
}
你可以试试这个:在一个片段中:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(
R.layout.shipment_accepted_fragment,
container, false);
String email = "(abc@gmail.com)";
TextView shipment_email = (TextView) rootView
.findViewById(R.id.textview);
String text_shipment_email = "Hello"
+ " <font color='"
+ getResources().getColor(R.color.green_color) + "'>" + email
+ "</font>"
+ "Bye";
shipment_email.setText(Html.fromHtml(text_shipment_email),
TextView.BufferType.SPANNABLE);
}
我创建了一个这样的类:
import android.text.SpannableStringBuilder;
import android.text.style.CharacterStyle;
public class StyleableSpannableStringBuilder extends SpannableStringBuilder {
public StyleableSpannableStringBuilder appendWithStyle(CharacterStyle c, CharSequence text) {
super.append(text);
int startPos = length() - text.length();
setSpan(c, startPos, length(), 0);
return this;
}
public StyleableSpannableStringBuilder appendWithStyle(CharacterStyle [] c, CharSequence text) {
super.append(text);
int startPos = length() - text.length();
for (CharacterStyle c1 : c)
setSpan(c1, startPos, length(), 0);
return this;
}
}
这允许我做这样的事情:
private void buildTickerItem(DelayedQuoteServiceObject o)
{
Double lastPrice = Double.parseDouble(o.getValue("LastPrice"));
Double dayChange = Double.parseDouble(o.getValue("DayChange"));
Double percentChange = Double.parseDouble(o.getValue("PercentDayChange")) / 100;
if (o.isIndex() == true)
{
tickerTapeData.appendWithStyle(new StyleSpan(Typeface.BOLD),o.getDisplayName());
tickerTapeData.append(" "+ indexFormat.format(lastPrice) + " (");
if (dayChange >= 0)
tickerTapeData.appendWithStyle(new ForegroundColorSpan(Colours.getTickerPositive()), indexFormat.format(dayChange));
else
tickerTapeData.appendWithStyle(new ForegroundColorSpan(Color.RED), indexFormat.format(dayChange));
}
else
{
tickerTapeData.appendWithStyle(new StyleSpan(Typeface.BOLD), o.ticker);
tickerTapeData.append("@"+ dollarFormat.format(lastPrice) + " (");
if (dayChange >= 0)
tickerTapeData.appendWithStyle(new ForegroundColorSpan(Colours.getTickerPositive()), dollarFormat.format(dayChange));
else
tickerTapeData.appendWithStyle(new ForegroundColorSpan(Color.RED), dollarFormat.format(dayChange));
}
tickerTapeData.append("/");
if (dayChange >= 0)
tickerTapeData.appendWithStyle(new ForegroundColorSpan(Colours.getTickerPositive()), percentFormat.format(percentChange));
else
tickerTapeData.appendWithStyle(new ForegroundColorSpan(Color.RED), percentFormat.format(percentChange));
tickerTapeData.append(") ");
}
创建自动收报机。 工作得很好,并保持代码干净。
String text = "<font color=#000>this color is black </font><font color=#6ab04c>this color is green</font>";
textview.setText(Html.fromHtml(text));
真的很简单。 只需将这些添加到您的 value/string.xml 中
<String name = 'name_of_the_String'><font color='#000000'>This is black</font><font color='#ffffff'>This is white</font></string>
然后将该字符串的名称添加到您的 XML 资源文件中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.