Android implements text-to-image blending

Source: Internet
Author: User

In our projects, we often encounter the problem of mixed images and text. There are many ways to solve such problems, and the methods presented in this paper are not unique. There is only one way to find a better fit based on the actual scenario.

This article mainly through the XML layout to realize the picture and the text mix (arranges horizontally).


1. Use TextView to achieve picture and text mix,

The android:drawablebottom outputs a drawable below the text. Chip

If you specify a color, the background of text is set to that color. And the same time and background when used to cover the latter.
Android:drawableleft on the left side of the text output a drawable, slice.
Android:drawablepadding set the interval between text and drawable (picture),

Used with Drawableleft, Drawableright, Drawabletop, Drawablebottom, can be set to negative. Use alone without effect.


Android:drawableright output a drawable to the right of text.


The android:drawabletop outputs a drawable directly above the text.


<span style= "FONT-SIZE:18PX;" >        <textview            android:id= "@+id/my_tv"            android:layout_width= "wrap_content"            android:layout_ height= "Wrap_content"            android:text= "online"            android:textcolor= "#85898f"            android:layout_margintop= "5DP"            android:drawablepadding= "5DP"            android:drawableleft= "@drawable/user_online"/></span>

Among them, Android:drawablepaddingh very good to overcome the picture and text spacing problem.

2.TextView Dynamic Settings Picture

drawable drawable= context.getresources (). getdrawable (r.drawable.text_img);//When calling Setcompounddrawables. The Drawable.setbounds () method must be called, otherwise the picture does not display drawable.setbounds (0, 0, drawable.getminimumwidth (), Drawable.getminimumheight ( ); Textview.setcompounddrawables (drawable, NULL, NULL, NULL); Set left icon

3. Use Relativelayout (linearlayout) to join TextView and ImageView (Buttonview) to achieve

<span style= "FONT-SIZE:18PX;" >   <relativelayout        android:layout_width= "wrap_content"        android:layout_height= "Wrap_content" >        <imageview            android:id= "@+id/my_iv"            android:layout_width= "wrap_content"            android:layout_ height= "Wrap_content"             android:background= "@drawable/user_online"            android:layout_alignparentleft= "true"            android:layout_centervertical= "true"            android:layout_marginleft= "5DP"            />                <textview            android:id= "@+id/my_tv"            android:layout_width= "wrap_content"            android:layout_height= "Wrap_ Content "             android:layout_torightof=" @id/my_iv "            android:layout_centervertical=" true "            Android: layout_marginleft= "5DP"            />    </RelativeLayout></span>


In fact, you can also use Java code to achieve the mix of pictures and text.



Android implements text-to-image blending

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.