我希望通过.setText("")方法更改TextView视图的文本,同时着色文本的一部分(或使其为粗体,斜体,透明等),而不是其余部分.例如:
title.setText("Your big island <b>ADVENTURE!</b>";
Run Code Online (Sandbox Code Playgroud)
我知道上面的代码是不正确的,但它有助于说明我想要实现的目标.我该怎么做?
Ale*_*lov 196
使用跨度.
例:
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);
Run Code Online (Sandbox Code Playgroud)
sat*_*sat 46
title.setText(Html.fromHtml("Your big island <b>ADVENTURE!</b>"));
Run Code Online (Sandbox Code Playgroud)
Lui*_*uis 25
我希望这可以帮助你(它适用于多语言).
<string name="test_string" ><![CDATA[<font color="%1$s"><b>Test/b></font>]]> String</string>
Run Code Online (Sandbox Code Playgroud)
在您的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));
Run Code Online (Sandbox Code Playgroud)
这样,只有"测试"部分将被着色(和粗体).
liv*_*ove 16
这是一个示例,它将查找所有出现的单词(不区分大小写),并将其着色为红色:
String notes = "aaa AAA xAaax abc aaA xxx";
SpannableStringBuilder sb = new SpannableStringBuilder(notes);
Pattern p = Pattern.compile("aaa", Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(notes);
while (m.find()){
//String word = m.group();
//String word1 = notes.substring(m.start(), m.end());
sb.setSpan(new ForegroundColorSpan(Color.rgb(255, 0, 0)), m.start(), m.end(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
}
editText.setText(sb);
Run Code Online (Sandbox Code Playgroud)
Dav*_*son 15
如果您使用的是 Kotlin,则可以使用android-ktx库执行以下操作
val title = SpannableStringBuilder()
.append("Your big island ")
.bold { append("ADVENTURE") }
titleTextField.text = title
Run Code Online (Sandbox Code Playgroud)
该bold
是一个扩展功能SpannableStringBuilder
。您可以在此处查看文档,了解您可以使用的操作列表。
另一个例子:
val ssb = SpannableStringBuilder()
.color(green) { append("Green text ") }
.append("Normal text ")
.scale(0.5F) { append("Text at half size ") }
.backgroundColor(green) { append("Background green") }
Run Code Online (Sandbox Code Playgroud)
哪里green
是已解析的 RGB 颜色。
甚至可以嵌套跨度,因此您最终会得到类似嵌入式 DSL 的东西:
bold { underline { italic { append("Bold and underlined") } } }
Run Code Online (Sandbox Code Playgroud)
您的应用程序模块级别需要以下内容build.gradle
才能使其工作:
repositories {
google()
}
dependencies {
implementation 'androidx.core:core-ktx:0.3'
}
Run Code Online (Sandbox Code Playgroud)
您可以使用a Spannable
来为文本的某些部分提供某些方面.如果你愿意,我可以查一个例子.
啊,就在stackoverflow上.
TextView TV = (TextView)findViewById(R.id.mytextview01);
Spannable WordtoSpan = new SpannableString("I know just how to whisper, And I know just how to cry,I know just where to find the answers");
WordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 15, 30, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
TV.setText(WordtoSpan);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
77813 次 |
最近记录: |