The following xml code works for setting the width and height of image drawables inside items of a layer-list
for API 23+:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item><shape android:shape="rectangle"><solid android:color="@color/white"/></shape></item>
<item android:drawable="@drawable/my_drawable" android:gravity="center" android:bottom="100dp"
android:width="100dp" android:height="100dp" />
<item android:drawable="@drawable/drawable_text" android:gravity="center" android:top="50dp"
android:width="200dp" android:height="200dp" />
</layer-list>
However, the android:width
and android:height
inside <item>
only works for API levels from 23 and up. Without the explicit width and heigth values the drawables will overflow the screen, and if I use resized PNG images, the quality becomes poor.
I have searched about workarounds on Stackoverflow (here, here, and here) but none of the solutions seem to work.
- Adding
android:top
,android:bottom
,android:left
, andandroid:right
don't resize/set the height and width of thedrawable
, possibly because they are PNG images and not vector drawables. - Adding a
drawable
to ashape
(and then use thesize
tag) doesn't work.