是否可以在 Android 中更改字符串中某些字符的颜色?例如,将单词 android 中字母 d 和 r 的颜色更改为绿色。
<TextView
android:id="@+id/word"
android:gravity="center_vertical"
android:typeface="monospace"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="30pt"/>
最佳答案
您可以使用 html 而不是纯文本来初始化 TextView。 html 可以用颜色定义文本范围。参见 Set TextView text from html-formatted string resource in XML如何。
如果你不想使用 html,你可以使用 SpannableString .
关于安卓字符串颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9450981/