In our projects, we often encounter the problem of mixed images and text. There are many ways to solve this kind of problem, the method given in this paper is not unique, only according to the actual situation can find a more suitable method.
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,
Android:drawablebottom output a drawable, slice below the text.
If a color is specified, the background of text is set to that color, and the latter is overridden when used with background.
android:drawableleft on the left side of the text output a drawable, slice.
android:drawablepadding sets the interval between text and drawable (picture),
Used with Drawableleft, Drawableright, Drawabletop, Drawablebottom, can be set to negative numbers, use alone no effect.
Android:drawableright output a drawable to the right of text.
The android:drawabletop outputs a drawable directly above the text.
<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"/>
Among them, the Android:drawablepaddingh very good solves the picture and the text spacing question.
2. Add TextView and ImageView (Buttonview) using Relativelayout (linearlayout) to achieve
<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>
In fact, you can also use Java code to achieve picture and text mix.