Hanbit the Developer
[Kotlin] Set indent to first line of TextView in android 본문
- Description
To do so, you should use Span, LeadingMarginSpan.
In a nutshell, Span is used to set text style in detail, and LeadingMarginSpan is one of the styles that is related to margin, especially can set margins of first and rest separately.
First of all, initialize your span which will be assigned to textView.
val spannable = SpannableString(yourTextView.text)
And, define LeadingMarginSpan like below.
val span = LeadingMarginSpan.Standard(50, 0)
The first parameter(50) is indent for first line, and the seconds is for rest lines.
Next, use setSpan() to apply LeadingMarginSpan to Span like this.
spannable.setSpan(span, 0, spannable.count(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
Finally assign Span to textView!
yourTextView.text = spannable
- Code
The integrated codes is as follows.
val spannable = SpannableString(yourTextView.text)
val span = LeadingMarginSpan.Standard(50, 0)
spannable.setSpan(span, 0, spannable.count(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
yourTextView.text = spannable
- Result
- Documents
https://developer.android.com/guide/topics/text/spans
https://developer.android.com/reference/android/text/style/LeadingMarginSpan
'Android' 카테고리의 다른 글
[Kotlin] Get URI from byte array in android (0) | 2021.08.19 |
---|---|
[Kotlin] How to make 'instagram comment layout' using Span. (0) | 2021.08.10 |
[Kotlin] TextView의 더보기 기능 구현 (0) | 2021.08.08 |
[Retrofit2] Send Post Request With Empty Body (0) | 2021.07.21 |
[Retrofit2 / OkHttpClient] Send Post Request With JWT(Bearer) Token (0) | 2021.07.21 |