简体   繁体   English

将Html和Set文本转换为Textview

[英]Convert Html and Set text to Textview

I am trying to convert a html String and set it to a TextView but I couldn't do it exactly. 我试图转换一个HTML字符串并将其设置为TextView但我无法完全做到。

Here is my String, "Hello, %1$s! You have <b>%2$d new messages</b>" 这是我的字符串, “你好,%1 $ s!你有<b>%2 $ d新消息</ b>”

I am using textview.setText(Html.fromHtml(myString)); 我正在使用textview.setText(Html.fromHtml(myString)); which produces me an output with Html Tags instead of plain text. 它生成一个带有Html标签而不是纯文本的输出。 Can anyone help me with this? 谁能帮我这个?

Any help is much appreciated. 任何帮助深表感谢。 Thanks 谢谢

Refer to this question . 请参阅此问题

try: 尝试:

textView.setText(Html.fromHtml(myString), TextView.BufferType.SPANNABLE);

This may be helpful to you: 这可能对您有所帮助:

Spanned marked_up = Html.fromHtml(myString);
textview.setText(marked_up.toString(),BufferType.SPANNABLE);

尝试使用版本的setText并使用SPANNABLE缓冲区类型

A bit late but extend TextView and override SetText like so: 有点晚但延伸TextView并覆盖SetText,如下所示:

public override void SetText(ICharSequence text, BufferType type)
{
    base.SetText(Html.FromHtml(text.ToString()), BufferType.Spannable);
}

Then you can just use your TextView instead of the regular one in your axml. 然后你可以使用你的TextView而不是你的axml中的常规TextView。

even later... Html.fromHtml is deprecated now: 甚至更晚......现在不推荐使用Html.fromHtml:

textView.setText(
    Html.fromHtml(model.getValue(), Html.FROM_HTML_MODE_COMPACT), 
    TextView.BufferType.SPANNABLE);

Html.FROM_HTML_MODE_COMPACT worked well for me, explore the other flags for different HTML parsing behaviour. Html.FROM_HTML_MODE_COMPACT对我有用,探索不同HTML解析行为的其他标志。

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

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