我有简单的HTML : html
<h2>Title</h2><br> <p>description here</p>
我想在TextView
显示HTML样式的文本。 这该怎么作? 服务器
看看这个: https : //stackoverflow.com/a/8558249/450148 布局
这也很不错!! ui
<resource> <string name="your_string">This is an <u>underline</u> text demo for TextView.</string> </resources>
它仅适用于少数标签。 spa
简单地使用Html.fromHtml("html string")
。 这会奏效。 若是字符串有像<h1>
这样的标签,则会出现空格。 但咱们没法消除这些空间。 若是您仍想删除空格,则能够删除字符串中的标记,而后将字符串传递给方法Html.fromHtml("html string");
。 一般这些字符串来自服务器(动态)但不常常,若是最好将字符串传递给方法,而不是尝试从字符串中删除标记。 code
如下代码为我提供了最好的结果。 htm
TextView myTextview = (TextView) findViewById(R.id.my_text_view); htmltext = <your html (markup) character>; Spanned sp = Html.fromHtml(htmltext); myTextview.setText(sp);
您须要使用Html.fromHtml()
在XML字符串中使用HTML。 简单地在布局XML中引用带有HTML的String将不起做用。 ip
这是你应该作的: 字符串
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { textView.setText(Html.fromHtml("<h2>Title</h2><br><p>Description here</p>", Html.FROM_HTML_MODE_COMPACT)); } else { textView.setText(Html.fromHtml("<h2>Title</h2><br><p>Description here</p>")); }
String value = "<html> <a href=\"http://example.com/\">example.com</a> </html>"; SiteLink= (TextView) findViewById(R.id.textViewSite); SiteLink.setText(Html.fromHtml(value)); SiteLink.setMovementMethod(LinkMovementMethod.getInstance());