14
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

HtmlCompatを使ってみる

Posted at

HTMLタグでTextViewを装飾できるHtml.fromHtmlがdeprecatedとなり、
androidxからHtmlCompatというクラスが追加されたようだ

さっそく使ってみる

準備

app/build.gradleにandroidx.appcompatを追加

app/build.gradle
dependencies {
+   implementation 'androidx.appcompat:appcompat:1.0.0-beta01'

実装

公式ドキュメントでHtmlCompatを見るとPublic methods
fromHtml(String source, int flags)
とある。

flagsにはとりあえずFROM_HTML_MODE_COMPACTを使ってみる。
「ブロックレベルの要素を改行(改行文字)で区切る」らしい

val html = """
    <html>
        <p>こんにちは、本日は<font color="#ff0000">晴天</font>なり
        <p>これが<u>アンダーライン</u>だ</p>
        <b><font color='red'>台風が</font><b/> 日本に <b><font color='red'>やってきた</font></b>
    </html>
""".trimIndent()

// HtmlCompatの出番!!!
text_view.text = HtmlCompat.fromHtml(html, FROM_HTML_MODE_COMPACT)

2018-08-08-18-55-29.png

できた、簡単だね

以上、現場からお伝えしました。

リンク

HtmlCompat | Android Developers

14
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?