Home Web Front-end HTML Tutorial 防微博内容展示,使用Html.fromHtml(),解决内容不能换行的问题_html/css_WEB-ITnose

防微博内容展示,使用Html.fromHtml(),解决内容不能换行的问题_html/css_WEB-ITnose

Jun 24, 2016 am 11:38 AM

 

       使用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);}}
Copy after login

        其中主要用到的类有 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;}
Copy after login
六、 总结

        来自地球的小伙伴们,如上只是这个小问题的解决思路,聪明的你一定会举一反三,在parseContent中去充分发挥,解决其他的问题,今天是七夕,祝情人节快乐奥,O(∩_∩)O哈哈~



    






Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the purpose of the <progress> element? What is the purpose of the <progress> element? Mar 21, 2025 pm 12:34 PM

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

What is the purpose of the <datalist> element? What is the purpose of the <datalist> element? Mar 21, 2025 pm 12:33 PM

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

What are the best practices for cross-browser compatibility in HTML5? What are the best practices for cross-browser compatibility in HTML5? Mar 17, 2025 pm 12:20 PM

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

What is the purpose of the <meter> element? What is the purpose of the <meter> element? Mar 21, 2025 pm 12:35 PM

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

How do I use the HTML5 <time> element to represent dates and times semantically? How do I use the HTML5 <time> element to represent dates and times semantically? Mar 12, 2025 pm 04:05 PM

This article explains the HTML5 &lt;time&gt; 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

How do I use HTML5 form validation attributes to validate user input? How do I use HTML5 form validation attributes to validate user input? Mar 17, 2025 pm 12:27 PM

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

What is the viewport meta tag? Why is it important for responsive design? What is the viewport meta tag? Why is it important for responsive design? Mar 20, 2025 pm 05:56 PM

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.

What is the purpose of the <iframe> tag? What are the security considerations when using it? What is the purpose of the <iframe> tag? What are the security considerations when using it? Mar 20, 2025 pm 06:05 PM

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

See all articles