简体   繁体   English

如何使字符串资源中的单词在android中显示红色?

[英]How to make the a word in a string resource to display red color in android?

I hope that the word "Click here" display red color, but the following code don't work, how can I do ? 我希望“点击此处”这个词显示红色,但下面的代码不起作用,我该怎么办? Thanks! 谢谢!

TextView mLink = (TextView) findViewById(R.id.tvMyLink);
Spanned my=android.text.Html.fromHtml(getString(R.string.mylink));   
mLink.setText(my);


<string name="mylink"><span style="color:#FF0000;">Click here </span> for the latest news!</string>

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

尝试这个。

mLink.setText(Html.fromHtml("<font color=\"#FF0000\"> Click here " + "</font>" + "for the latest news!"));

You can format the String with html's font-color property then pass it to the method 您可以使用html的font-color属性格式化String,然后将其传递给方法

Html.fromHtml(your text here) Html.fromHtml(您的文字在这里)

String text = "<font color=#cc0029>First Color</font> <font 
color=#ffcc00>Second Color</font>";
yourtextview.setText(Html.fromHtml(text));

Don't forget to escape userinput using Html.escapeHtml(str) 不要忘记使用Html.escapeHtml(str)来逃避userinput

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM