我希望通过.setText("")方法更改TextView视图的文本,同时对文本的一部分进行着色(或使其变为粗体、斜体、透明等),而不是对其余部分进行着色。例如:
title.setText("Your big island <b>ADVENTURE!</b>";
我知道上面的代码是不正确的,但它有助于说明我想要实现的目标。我该怎么做呢?
发布于 2011-02-04 19:19:51
使用spans。
示例:
final SpannableStringBuilder sb = new SpannableStringBuilder("your text here");
// Span to set text color to some RGB value
final ForegroundColorSpan fcs = new ForegroundColorSpan(Color.rgb(158, 158, 158));
// Span to make text bold
final StyleSpan bss = new StyleSpan(android.graphics.Typeface.BOLD);
// Set the text color for first 4 characters
sb.setSpan(fcs, 0, 4, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
// make them also bold
sb.setSpan(bss, 0, 4, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
yourTextView.setText(sb);
发布于 2011-02-04 19:16:03
title.setText(Html.fromHtml("Your big island <b>ADVENTURE!</b>"));
发布于 2014-09-18 20:49:37
我希望这对你有帮助(它适用于多语言)。
<string name="test_string" ><![CDATA[<font color="%1$s"><b>Test/b></font>]]> String</string>
在你的java代码中,你可以这样做:
int color = context.getResources().getColor(android.R.color.holo_blue_light);
String string = context.getString(R.string.test_string, color);
textView.setText(Html.fromHtml(string));
这样,只有"Test“部分会有颜色(和粗体)。
https://stackoverflow.com/questions/4897349
复制相似问题