Android implements clickable textview

summary

During Android development, we often encounter incomplete display of textview.

In this case, the usual processing is:

Scheme 1: add Android: ellipsize attribute to textview, and use ellipsis to replace the endless parts. In scheme 2, the textview adopts the riding lamp effect to make it scroll and display all text contents. For scheme 1, if you want to view the omitted content, how to implement it? Usually, a clickable icon is added behind or below the textview text to expand and shrink the textview. As shown below:

Shrinkage state

Expanded state

Implementation principle

For the above effects, the general realization idea is:

The following code is used to describe the implementation process in detail:

Add view height listener to textview

Use spannablestring

In spannablestring, we can add icons to textview by setting imagespan, but ordinary imagespan can't respond to click events and can't set the position of pictures. So how can we implement an imagespan that can respond to click events and set the position of pictures?

Step 1:

Create a clickableimagespan class that has all the properties of imagespan and can be clicked. The picture is vertically centered.

Step 2:

Create a clickablemovementmethod (modify the ontouchevent method of linkmovementmethod) to support clickableimagespan.

Set the modified spannablestring to textview

Call in Activity

Full demo link: expandabletextview

Other methods are used to implement scalable textview (using setmaxlines method). Portal:

How to write a textview that can be expanded? Android textview uses one: scaling effect

Reference article:

Use spannablestring and imagespan to insert pictures in textview, customize clickable imagespan, and build "view" in textview“

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>