【问题标题】:Android string colorsAndroid 字符串颜色
【发布时间】:2012-03-16 02:20:11
【问题描述】:

是否可以在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"/>

【问题讨论】:

  • 我只是在使用我在上面插入的主要问题中的 XML TextView。我使用 word.setText("Android") 在主代码中显示文本,但我需要将一些字符更改为不同的颜色。

标签: android string colors


【解决方案1】:

您可以使用 html 而不是纯文本来初始化 TextView。 html 可以用颜色定义文本跨度。具体方法见Set TextView text from html-formatted string resource in XML

如果您不想使用 html,可以使用 SpannableString

【讨论】:

    猜你喜欢
    • 2016-05-10
    • 1970-01-01
    • 2011-06-28
    • 1970-01-01
    • 2011-05-12
    • 1970-01-01
    • 1970-01-01
    • 2012-04-13
    相关资源
    最近更新 更多