温馨提示:本文翻译自stackoverflow.com,查看原文请点击:android - Color part of string in textView in XML
android android-layout xml android-studio

android - 为XML的textView中的字符串上色

发布于 2020-03-27 12:05:58

我正在尝试使用@string在TextView中显示一些文本,问题是我似乎无法更改部分文本的颜色。我不想像其他所有问题一样使用Java来执行此操作,因为这是默认的TextView文本。

我试过使用<string name="myString"><font color="#00ff00">My String</font></string>font fgcolor="#00ff00"但它们似乎没有用。

查看更多

查看更多

提问者
kriskotoo BG
被浏览
109
2,820 2019-07-04 03:22

我已经尝试过使用您给定的字符串,它对我来说很有效。这是一个例子

  • <TextView>在任何布局内创建

    <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/text" />

  • textstrings.xml文件创建字符串

    <string name="text"><font color="#00ff00">Hello</font> <font color="#FF0000">World</font></string>

  • 你可以看到结果

    在此处输入图片说明