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