在android中相同文本视图中的Text的2种不同颜色

4 android textcolor textview

我有一个TextView小部件,如下所示

<TextView 
        android:text="@string/inputText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" />
Run Code Online (Sandbox Code Playgroud)

inputText在Strings.xml中定义为

<String name="inputText">The value of input is positive now</String>
Run Code Online (Sandbox Code Playgroud)

现在,我希望整个文本以棕色显示,而只有绿色显示"正".

有没有办法这样做?简而言之,我的问题是同时对同一文本视图进行多重着色

M4t*_*X3r 15

这是此问题的一个dublicate 更改TextView中一个单词的文本颜色.

String first = "This word is ";
String next = "<font color='#EE0000'>red</font>";
t.setText(Html.fromHtml(first + next));
Run Code Online (Sandbox Code Playgroud)


Raw*_*ode 7

你可以CDATA在你的内部用你strings.xml的字符串存储一些HTML格式,并Html.fromHTML在你的内容中显示TextView.

strings.xml中

<string name="inputText">
    <![CDATA[
      <p>The value of input is <font color='#00ff00'>positive</font> now.</p>
    ]]>
</string>
Run Code Online (Sandbox Code Playgroud)

Java代码

myTextView.setText(Html.fromHtml(getString(R.string.inputText));
Run Code Online (Sandbox Code Playgroud)


Shi*_*hiv 2

使用这种方法:用 html 格式化字符串

String text = "<font color=#cc0029>Text with Color first</font> <font color=#ffcc00>Text with Color second</font>";
yourtextview.setText(Html.fromHtml(text));
Run Code Online (Sandbox Code Playgroud)