6

I can't figure out how to set the alignment of text and its compound drawable (smaller than text) in a TextView. It seems the default alignment is center, but I need to align them by the edge.

PS: The android:gravity works great if compound drawable is larger than text, but not if smaller.

Oasis Feng
  • 7,490
  • 3
  • 31
  • 44

2 Answers2

1

It sounds like you're using an image with fixed dimensions for android:drawableLeft.

Try using a nine-patch or defining a drawable through an XML resource file. In this way your drawable's dimensions will stretch to align in the TextView the way that you want it to.

Paul Lammertsma
  • 37,593
  • 16
  • 136
  • 187
  • Good idea for nine-patch! But I'm still slightly confused about the second solution. Take "android:drawableLeft" for example, how to write the bridge XML drawable to align the text with the compound image by top? – Oasis Feng Jan 30 '13 at 15:22
  • I haven't actually tried it, but you should be able to use [`` in conjunction with `android:gravity`](http://developer.android.com/guide/topics/resources/drawable-resource.html#XmlBitmap). – Paul Lammertsma Feb 02 '13 at 00:10
0

I had a similar problem in that I wanted to use setCompoundDrawables to set a top drawable for a floating hint, however TextView (EditText) sets the top drawable to center, irrespective of what its width is.

I am using a custom Drawable, and inside its draw method, I am grabbing the clipBounds, and translating on its top,left to get back to the TextView's 0,0 position.

The other way to do it easily is to getMatrix and mapPoints from 0,0 then take the negative of them and translate back to 0,0 that way.

Peterm
  • 1
  • 1