Is it possible to have multiple styles inside a TextView?

后端 未结 18 2091
醉梦人生
醉梦人生 2020-11-21 17:34

Is it possible to set multiple styles for different pieces of text inside a TextView?

For instance, I am setting the text as follows:

tv.setText(line         


        
18条回答
  •  滥情空心
    2020-11-21 17:54

    Yes, it is possible using SpannedString. If you are using Kotlin, it becomes even easier to do by using core-ktx, as it provides a domain-specific-language (DSL) for doing this:

        val string: SpannedString = buildSpannedString {
            bold {
                append("1111")
            }
            append("Devansh")     
        }
    

    More options provided by it are:

    append("Hello There")
    bold {
        append("bold")
        italic {
            append("bold and italic")
            underline {
                append("then some text with underline")
            }
        }
    }
    

    At last, you can just to:

    textView.text = string
    

提交回复
热议问题