To expand on Romain Guy's answer, here is an example.
Before:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:padding="5dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="My Compound Button" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/my_drawable" />
</LinearLayout>
After:
<TextView
android:layout_marginTop="10dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="My Compound Button"
android:drawableRight="@drawable/my_drawable" android:padding="5dp" />
Merge the TextView
and the ImageView
into one, by using TextView's setCompoundDrawable*()
methods, or using android:drawableLeft
.
Thought I would try to get some extra puntos for this as well: you can add padding between the image and the text using android:drawablePadding
. https://stackoverflow.com/a/6671544/1224741
Sometimes it is possible to replace ImageView
(or multiple) and TextView
with one TextView
with compound drawable(s). There are NOT many parameters which can be applied to compound drawable using native API and this TextViewRichDrawable library, but if you can manage one TextView instead of using LinearLayout you should definitely use it.
The list of attributes and parameters which can be applied to compound drawables:
Size: (YES, really):
<com.tolstykh.textviewrichdrawable.TextViewRichDrawable
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Some text"
app:compoundDrawableHeight="24dp"
app:compoundDrawableWidth="24dp"/>
Even set vector resource as drawable:
<com.tolstykh.textviewrichdrawable.TextViewRichDrawable
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Some text"
app:drawableTopVector="@drawable/some_vector_drawble"
app:drawableEndVector="@drawable/another_vector_drawable" />
Drawable's Padding using native API android:drawablePadding
-> link
Here is an example:
Add tools:ignore="UseCompoundDrawables"
to <LinearLayout>
.
A LinearLayout
which contains an ImageView
and a TextView
can
be more efficiently handled as a compound drawable (a single
TextView
, using the drawableTop
, drawableLeft
, drawableRight
and/or drawableBottom
attributes to draw one or more images
adjacent to the text).
If the two widgets are offset from each other with margins, this
can be replaced with a drawablePadding
attribute.
There's a lint quickfix to perform this conversion in the Eclipse plugin.
From: Android Official API docs!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With