How to use custom ellipsis in Android TextView

前端 未结 5 1778
陌清茗
陌清茗 2020-12-31 09:55

I have a TextView with maxlines=3 and I would like to use my own ellipsis, instead of

\"Lore ipsum ...\"

I need

\"Lore ips         


        
5条回答
  •  借酒劲吻你
    2020-12-31 10:22

    Here is a solution for Kotlin.

    The yourTextView.post{} is necessary because the textview won't be ellipsized until after it is rendered.

      val customSuffix = "... [See more]"
      yourTextView.post {
        if (yourTextView.layout.getEllipsisStart(-1) != -1) {
          val newText = yourTextView.text.removeRange(
              yourTextView.layout.getEllipsisStart(-1) - customSuffix.length, yourTextView.text.length
          )
          yourTextView.text = String.format("%s%s", newText, customSuffix)
        }
      }
    

提交回复
热议问题