防微博内容展示,使用Html.fromHtml(),解决内容不能换行的问题_html/css_WEB-ITnose
使用Html.fromHtml(),解决内容不能换行的问题,模仿微博内容展示效果。
一、需求要实现的效果
如下图中箭头指向的微博内容部分,包含超链接,点击超链接后要跳转到相应的WebView页面。(csdn上传图片试了好多遍也不成功,大家脑补一下吧,辛苦了)。
二、 实现思路
首先获取网络数据,通过Html.fromHtml()解析获取到的数据,这时超链接、段落符
、换行符
等将会被展示成对应的表现形式,就会出现上图所示的效果。然后我们解决第二个问题,点击跳转的问题,直接上代码。
/** * 设置TextView中URL由内嵌浏览器打开 * @param context * @param textView*/public static void setOpenUrlByBrowser(Context context, TextView textView) {textView.setMovementMethod(LinkMovementMethod.getInstance());CharSequence text = textView.getText();if (text instanceof Spannable) {int end = text.length();Spannable sp = (Spannable) textView.getText();URLSpan[] urls = sp.getSpans(0, end, URLSpan.class);SpannableStringBuilder style = new SpannableStringBuilder(text);style.clearSpans();for (URLSpan url : urls) {MyURLSpan myURLSpan = new MyURLSpan(context, url.getURL());style.setSpan(myURLSpan, sp.getSpanStart(url), sp.getSpanEnd(url), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);}textView.setText(style);}}
其中主要用到的类有 Spannable、SpannableStringBuilder,如果有不了解两类的同学,可以Google这连个类,后面有时间也会分析这两个类的实现。至于MyURLSpan是自定义的一个超链接点击之后的操作类,在这里实现界面的跳转。So,我们实现了展示和跳转。
三、 遇到的问题
如上编码之后,我遇到了文本不能换行的问题,如在我的应用中我发的一遍帖子带有换行的帖子,但是在帖子详情页面没有换行显示。
四、 分析原因
查看服务端返回的数据,发现时这样的。 “ 这是一个测试用的帖子\n我想测试他能不能换行\n”,于是找到了原因,html不能解析“\n”。
五、 解决办法
最吊炸天的地方来了,将所有的“\n”转换成“
”,代码如下,这样就可以解决问题。
private String parseContent(String content) {if(StringUtil.isNotEmpty(content)){content = content.replace("\n","<br>");}return content;}
来自地球的小伙伴们,如上只是这个小问题的解决思路,聪明的你一定会举一反三,在parseContent中去充分发挥,解决其他的问题,今天是七夕,祝情人节快乐奥,O(∩_∩)O哈哈~

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The article discusses the HTML <progress> element, its purpose, styling, and differences from the <meter> element. The main focus is on using <progress> for task completion and <meter> for stati

The article discusses the HTML <datalist> element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

The article discusses the HTML <meter> element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates <meter> from <progress> and ex

This article explains the HTML5 <time> element for semantic date/time representation. It emphasizes the importance of the datetime attribute for machine readability (ISO 8601 format) alongside human-readable text, boosting accessibilit

The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

The article discusses the viewport meta tag, essential for responsive web design on mobile devices. It explains how proper use ensures optimal content scaling and user interaction, while misuse can lead to design and accessibility issues.

The article discusses the <iframe> tag's purpose in embedding external content into webpages, its common uses, security risks, and alternatives like object tags and APIs.
