使用textview显示html,如何在TextView中显示HTML?

You need to use Html.fromHtml() to use HTML in your XML Strings. Simply referencing a String with HTML in your layout XML will not work.

This is what you should do:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {

textView.setText(Html.fromHtml("

Title

Description here

", Html.FROM_HTML_MODE_COMPACT));

} else {

textView.setText(Html.fromHtml("

Title

Description here

"));

}

setText(Html.fromHtml(bodyData)) is deprecated after api 24. Now you have to do this:

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {

tvDocument.setText(Html.fromHtml(bodyData,Html.FROM_HTML_MODE_LEGACY));

} else {

tvDocument.setText(Html.fromHtml(bodyData));

}

It is pretty good too!!

This is an underline text demo for TextView.

It works only for few tags.

If you want to be able to configure it through xml without any modification in java code you may find this idea helpful. Simply you call init from constructor and set the text as html

public class HTMLTextView extends TextView {

... constructors calling init...

private void init(){

setText(Html.fromHtml(getText().toString()));

}

}

xml:

android:text="@string/about_item_1"/>

The below code gave best result for me.

TextView myTextview = (TextView) findViewById(R.id.my_text_view);

htmltext = ;

Spanned sp = Html.fromHtml(htmltext);

myTextview.setText(sp);

If you are trying to show HTML from a string resource id, the formatting may not show up on screen. If that is happening to you, try using CDATA tags instead:

strings.xml:

Title

Description here

]]>

...

MainActivity.java:

text.setText(Html.fromHtml(getString(R.string.sample_string));

See this post for further details.

String value = " example.com ";

SiteLink= (TextView) findViewById(R.id.textViewSite);

SiteLink.setText(Html.fromHtml(value));

SiteLink.setMovementMethod(LinkMovementMethod.getInstance());

If you just want to display some html text and don't really need a TextView, then take a WebView and use it like following:

String htmlText = ...;

webview.loadData(htmlText , "text/html; charset=UTF-8", null);

This does not restrict you to a few html tags either.

It's worth mentioning that the method Html.fromHtml(String source) is deprecated as of API level 24. If that's your target API, you should use Html.fromHtml(String source, int flags) instead.

The best approach to use CData sections for the string in strings.xml file to get a actual display of the html content to the TextView the below code snippet will give you the fair idea.

//in string.xml file

Welcome, to the forthetyroprogrammers blog Logged in as:]]> %1$s.

//and in Java code

String welcomStr=String.format(getString(R.string.welcome_text),username);

tvWelcomeUser.setText(Html.fromHtml(welcomStr));

CData section in string text keeps the html tag data intact even after formatting text using String.format method. So, Html.fromHtml(str) works fine and you’ll see the bold text in Welcome message.

Output:

Welcome, to your favorite music app store. Logged in as: username

I would like also to suggest following project: https://github.com/NightWhistler/HtmlSpanner

Usage is almost the same as default android converter:

(new HtmlSpanner()).fromHtml()

Found it after I already started by own implementation of html to spannable converter, because standard Html.fromHtml does not provide enough flexibility over rendering control and even no possibility to use custom fonts from ttf

Simple use Html.fromHtml("html string"). This will work. If the string has tags like

then spaces will come. But we cannot eliminate those spaces. If you still want to remove the spaces, then you can remove the tags in the string and then pass the string to the method Html.fromHtml("html string"); . Also generally these strings come from server(dynamic) but not often, if it is the case better to pass the string as it is to the method than try to remove the tags from the string.

String value = html value ....

mTextView.setText(Html.fromHtml(value),TextView.BufferType.SPANNABLE)

I have implemented this using web view. In my case i have to load image from URL along with the text in text view and this works for me.

WebView myWebView =new WebView(_context);

String html = childText;

String mime = "text/html";

String encoding = "utf-8";

myWebView.getSettings().setJavaScriptEnabled(true);

myWebView.loadDataWithBaseURL(null, html, mime, encoding, null);

Make a global method like:

public static Spanned stripHtml(String html) {

if (!TextUtils.isEmpty(html)) {

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {

return Html.fromHtml(html, Html.FROM_HTML_MODE_COMPACT);

} else {

return Html.fromHtml(html);

}

}

return null;

}

You can also use it in your Activity/Fragment like:

text_view.setText(stripHtml(htmlText));

I know this question is old. Other answers here suggesting Html.fromHtml() method. I suggest you to use HtmlCompat.fromHtml() from android.support.v4.text.HtmlCompat package. As this is backward compatible version of Html class.

Sample code:

import android.support.v4.text.HtmlCompat;

import android.text.Spanned;

import android.widget.TextView;

String htmlString = "

Hello World!

";

Spanned spanned = HtmlCompat.fromHtml(htmlString, HtmlCompat.FROM_HTML_MODE_COMPACT);

TextView tvOutput = (TextView) findViewById(R.id.text_view_id);

tvOutput.setText(spanned);

By this way you can avoid Android API version check and it's easy to use (single line solution).

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值