Detailed explanation of 4 ways to display pictures in Android textview

As we know, textview controls are generally used to display text, while images are generally displayed with ImageView controls.

Can textview display pictures? The answer is yes! Four common methods are listed below.

This method should be the most commonly used. Pictures are displayed at the top left and bottom right of textview. You can use Android: drawableleft Android: drawabletop Android: drawableright Android: drawablebottom

For example, we need to set pictures at the top of textview. The code is as follows:

This display method aligns the picture and text in the center. The corresponding method of this method is setcomposundrawableswithintrinsic boundaries:

design sketch:

If you think the picture is too close to the text, you can also set the spacing between them, which can be realized in XML or code:

perhaps

This method can display the pictures in the project, local sdcard and network. Of course, the network pictures must be downloaded to the local and then displayed.

Look at the code

As you can see, IC_ The IDs of launcher and apple are added to the IMG tag, and then the images are obtained by implementing the getdrawable () method in the imagegetter interface of HTML.

The renderings are as follows:

In order to simplify the code, we use Google's volley network framework to request images, and then save them to sdcard for display. This method is a little troublesome. See the code:

There are a lot of codes. Just figure out the process. First find C > locally. If you can't find it, download it through the network and save it to local C > to display local pictures.

The renderings are as follows:

This method is very simple. You can get the picture by creating an imagespan object, and then pass it as a parameter into the setspan method of spannablestring. Look at the code:

Note: the start and end values in the setspan (object what, int start, int end, int flags) method are text ranges replaced by pictures, and flags are used to identify whether to apply this effect to new characters before and after the text in the span range.

design sketch:

The principle of this method is to inherit textview and rewrite ondraw() to draw the picture directly onto the text, which will cause the picture to overlap with the text, and the spacing between them is difficult to control.

Then reference the custom control in the XML file:

design sketch:

The above are the four ways to display pictures in Android textview. Each way is your own application scenario. You can choose the correct way according to your own situation

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
分享
二维码
< <上一篇
下一篇>>