美文网首页
Html.fromHtml deprecated in Andr

Html.fromHtml deprecated in Andr

作者: beforenight | 来源:发表于2017-04-19 13:34 被阅读582次

Html.fromHtml deprecated in Android N


I am using Html.fromHtml to view html in a TextView.

Spanned result = Html.fromHtml(mNews.getTitle());
...
...
mNewsTitle.setText(result);

But Html.fromHtml is now deprecated in Android N+

What/How do I find the new way of doing this?


You have to add a version check and use the old method on Android M and below, on Android N and higher you should use the new method. If you don't add a version check your app will break on lower Android versions. You can use this method in your Util class.

@SuppressWarnings("deprecation")
public static Spanned fromHtml(String html){
Spanned result;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
   result = Html.fromHtml(html,Html.FROM_HTML_MODE_LEGACY);
} else {
   result = Html.fromHtml(html);
}
return result;
}

Flag parameters:

public static final int FROM_HTML_MODE_COMPACT = 63;
public static final int FROM_HTML_MODE_LEGACY = 0;
public static final int FROM_HTML_OPTION_USE_CSS_COLORS = 256;
public static final int FROM_HTML_SEPARATOR_LINE_BREAK_BLOCKQUOTE = 32;
public static final int FROM_HTML_SEPARATOR_LINE_BREAK_DIV = 16;
public static final int FROM_HTML_SEPARATOR_LINE_BREAK_HEADING = 2;
public static final int FROM_HTML_SEPARATOR_LINE_BREAK_LIST = 8;
public static final int FROM_HTML_SEPARATOR_LINE_BREAK_LIST_ITEM = 4;
public static final int FROM_HTML_SEPARATOR_LINE_BREAK_PARAGRAPH = 1;
public static final int TO_HTML_PARAGRAPH_LINES_CONSECUTIVE = 0;
public static final int TO_HTML_PARAGRAPH_LINES_INDIVIDUAL = 1;

You can read more about the different flags on the Html class documentation.
https://developer.android.com/reference/android/text/Html.html#FROM_HTML_MODE_COMPACT

相关文章

网友评论

      本文标题:Html.fromHtml deprecated in Andr

      本文链接:https://www.haomeiwen.com/subject/snoczttx.html