public class Drawabletoplefttextview extends TextView {private Paint mpaint;private float ffontheight;private drawable[] drawables;private int leftMargin = 40; TODO this to get through code, cannot hard code public Drawabletoplefttextview (context context, AttributeSet attrs, int defstyle) {Super (context , Attrs, Defstyle); init ();} Public Drawabletoplefttextview (context context, AttributeSet Attrs) {Super (context, attrs); init ();} Public Drawabletoplefttextview (Context context) {super (context); init ();} private void Init () {mpaint = Getpaint ();//mpaint.settextsize (56.0f); Mpaint.setcolor (Color.parsecolor ("#cccccccc")) ; FontMetrics fm = mpaint.getfontmetrics () Ffontheight = (float) math.ceil (fm.descent-fm.ascent);d rawables = Getcompounddrawables ();} @Overrideprotected void OnDraw (canvas canvas) {if (drawables! = null) {drawable drawable = drawables[1];//TOPIF (DRAWABL E! = null) {final float texty = GetY () + drawable.getintrinsicheight () + Ffontheight-5;canvas.drawtext (GetText (). Tostrin g (), 0, texty, mpaint); canvas.cLiprect (Drawable.getbounds ()); Canvas.drawbitmap (Getbitmap (drawable), GetLeft () +leftmargin, GetTop (), mpaint); Drawable.draw (canvas); Canvas.save ();}} Super.ondraw (canvas);} Private final static Bitmap Getbitmap (drawable drawable) {bitmapdrawable BD = (bitmapdrawable) drawable;return Bd.getbitmap ();}}
<com.bluetooth.light.widget.drawabletoplefttextview android:id= "@+id/signal_text" android:layout_ Width= "Wrap_content" android:layout_height= "wrap_content" android:layout_alignparentright= "true" android:layout_marginright= "10DP" android:drawabletop= "@drawable/signal_logo" android:text= "@string/ Signal " android:textcolor=" #cccccccc " android:textsize=" 24sp "/>
Android custom TextView drawabletop icons left aligned with text ()