HtmlCompat.fromHtml(getString(R.string.string_kakao_warning_message), HtmlCompat.FROM_HTML_MODE_LEGACY);
안드로이드 개발하면서 TextView에 HTML 태그를 사용 할 일이 생겼습니다.
정확히는 텍스트안에 특정 텍스트는 색을 넣어야하는 상황이고, TextView를 나눠서 사용하는것보단 TextView를 태그로 변환해서 HTML 태그를 사용하는 방향으로 진행하였습니다.
안드로이드에서 Html 태그를 사용하는 방법은 다음과 같습니다.
Html.fromHtml(String, int) 메서드를 사용하고, API가 24이상 (Android 7.0 Nought)에서 androidx HtmlCompat의 fromHtml(String, int) 메서드를 사용합니다.
Html.fromHtml(String, int)는 android.text 패키지에 기본적으로 존재하므로 라이브러리를 추가할 필요는 없으며, HtmlCompat 또한 androidx의 일반적으로는 프로젝트 생성시 추가가 되어있으므로 바로 사용할 수 있지만, 없을경우에는 gradle에 다음과 같이 추가해줍니다.
dependencies {
implementation 'androidx.core:core:1.0.1
}
사용법은 다음과 같습니다.
Html.fromHtml(String, int) HtmlCompat.fromHtml(String, int)
첫번째 파라미터에는 HTML로 표현할 문자열 (HTML 태그가 포함된 문자열)을 추가해주고, 두번째 파라미터에는 플래그입니다.)
플래그는 다음과 같이 사용할 수 있습니다.
| Constants | |
| int | FROM_HTML_MODE_COMPACT Flags for fromHtml(String, int, ImageGetter, TagHandler): Separate block-level elements with line breaks (single newline character) in between. |
| int | FROM_HTML_MODE_LEGACY Flags for fromHtml(String, int, ImageGetter, TagHandler): Separate block-level elements with blank lines (two newline characters) in between. |
| int | FROM_HTML_OPTION_USE_CSS_COLORS Flag indicating that CSS color values should be used instead of those defined in Color. |
| int | FROM_HTML_SEPARATOR_LINE_BREAK_BLOCKQUOTE Flag indicating that texts inside <blockquote> elements will be separated from other texts with one newline character by default. |
| int | FROM_HTML_SEPARATOR_LINE_BREAK_DIV Flag indicating that texts inside <div> elements will be separated from other texts with one newline character by default. |
| int | FROM_HTML_SEPARATOR_LINE_BREAK_HEADING Flag indicating that texts inside <h1>~<h6> elements will be separated from other texts with one newline character by default. |
| int | FROM_HTML_SEPARATOR_LINE_BREAK_LIST Flag indicating that texts inside <ul> elements will be separated from other texts with one newline character by default. |
| int | FROM_HTML_SEPARATOR_LINE_BREAK_LIST_ITEM Flag indicating that texts inside <li> elements will be separated from other texts with one newline character by default. |
| int | FROM_HTML_SEPARATOR_LINE_BREAK_PARAGRAPH Flag indicating that texts inside <p> elements will be separated from other texts with one newline character by default. |
예제코드는 다음과 같습니다.
string.xml에 태그가 포함된 텍스트를 넣어서 다음과 같이 사용하고 있습니다.
[string.xml]
<string name="from_html_message"><font color='blue'>테스트</font> : 테스트입니다.<br>테스트입니다.<br></string>
[Code]
HtmlCompat.fromHtml(getString(R.string.from_html_message), HtmlCompat.FROM_HTML_MODE_LEGACY);
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/tv_from_html_message"
android:text="@string/from_html_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</androidx.appcompat.widget.AppCompatTextView>
참고 URL
'Development > Android' 카테고리의 다른 글
| 안드로이드 현재시간 가져오기 (DateUtils) (0) | 2021.09.15 |
|---|---|
| Android Studio 최근 프로젝트 삭제하기 (0) | 2021.08.14 |
| 안드로이드 패키지명(PackageName)으로 설치여부 확인하는 방법 (0) | 2020.09.07 |
| 안드로이드 설치된 앱 버전 읽어오기 (0) | 2020.09.07 |
| 안드로이드 가로모드(혹은 세로모드) 고정하는 방법. (0) | 2020.09.05 |