[英]How to make the a word in a string resource to display red color in android?
我希望“點擊此處”這個詞顯示紅色,但下面的代碼不起作用,我該怎么辦? 謝謝!
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!"));
您可以使用html的font-color屬性格式化String,然后將其傳遞給方法
Html.fromHtml(您的文字在這里)
String text = "<font color=#cc0029>First Color</font> <font
color=#ffcc00>Second Color</font>";
yourtextview.setText(Html.fromHtml(text));
不要忘記使用Html.escapeHtml(str)來逃避userinput
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.