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

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. Google确认下一个Android版本将不会使用O
  2. Android消息处理惩罚机制(Handler、Looper
  3. https://source.android.com/devices/blu
  4. android 默认签名 debug签名
  5. Android(安卓)source build/envsetup.sh
  6. Android(安卓)轻松实现语音朗读
  7. Android进程保活总结
  8. 精通android体系架构、mvc、常见的设计模
  9. 包建强的培训课程(2):Android与设计模式
  10. Android(安卓)LOG机制的实现的详细解读