Explain in detail the application practice of Android spannablestring multi line graphic mixing

Textview should be familiar to everyone. Text display controls! Just use textview to display ordinary text. OK, it's very simple. Those who get started with Android can look at it outside the door. Ha ha, I'm kidding. What if the design requires inserting emoticons in textview like wechat chat emoticons in the development requirements? Some friends will say, "I can add pictures to textview. It's drawableleft and drawableright!" well, it's OK. It's a method, but there is a limitation. First, pictures can only be set at both ends of textview. Second, only one picture can be set at both ends. What if the picture is in the middle of the text? There's nothing you can do. If you can use spannablestring, it's not difficult to solve this problem. It's just so so.

Therefore, whether you are experiencing the above problems or not, please stop and read this short article carefully and patiently. It can not only easily realize the above design requirements, but also harvest other cool effects, which may help you solve the problems you are troubled with.

First of all, let's take a look at the effect picture and pay attention to how the top of the picture is realized? Of course, you can also set his click events, such as those with videos or other classes. We won't introduce more here. Just look at the specific needs of the project.

design sketch

If one line is involved, we only need to add an ImageView after the text, but it is not good if multiple lines are displayed

So what is spannablestring?

Spannablestring, like string, is a string type. Similarly, textview can directly set spannablestring as display text. The difference is that spannablestring can display strings in various forms and styles by using its method setspan. The important thing is to specify the set interval, That is, format the substring within the specified subscript interval of the string.

The setspan (object what, int start, int end, int flags) method requires the user to enter four parameters. What indicates the format to be set, which can be foreground color, background color, clickable text, etc. start indicates the start subscript of the substring to be formatted. Similarly, end indicates the end subscript. Flags is an identification. There are four attributes:

Now, let's see how to achieve this effect in the code?

After we get the title returned by the interface, we pass the picture we need through drawable drawable = getresources(). Getdrawable (r.mipmap. Icon_top); drawable.setBounds(0,drawable.getIntrinsicHeight());// Don't fill in the int parameter for the last two digits here, otherwise the display will be irregular on the large screen mobile phone

After you get the picture, you can easily and quickly achieve this effect by setting the size and position you want.

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