Html.fromHtml deprecated in Android(安卓)N
16lz
2021-01-26
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
更多相关文章
- 代码中设置drawableleft
- android 3.0 隐藏 系统标题栏
- Android开发中activity切换动画的实现
- Android(安卓)学习 笔记_05. 文件下载
- Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
- 技术博客汇总
- android 2.3 wifi (一)
- AndRoid Notification的清空和修改
- Android中的Chronometer