Incorrect items sizing in layer-list on pre-lollipop
Its best not to mention the height
and width
attributes in layer-list
. When you use this as drawable in let's say an ImageView (the width and height of the ImageView will override the one's mentioned in item/shape).
You can use top, bottom, left, right
to align your items in layer-list.
Note: height and width attributes of item is supported only from API 23.
Here is a working example (Tested in Android 7.0 and Android 4.4):
sample.xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/menu" android:left="16dp" android:right="16dp" android:gravity="center"> <bitmap android:src="@drawable/menu_icon" /> </item> <item android:top="8dp" android:left="56dp" android:right="24dp" android:bottom="42dp"> <shape android:shape="oval"> <solid android:color="@android:color/holo_red_dark"/> </shape> </item></layer-list>
main.xml
<ImageView android:layout_width="72dp" android:layout_height="72dp" android:src="@drawable/sample" android:scaleType="fitCenter"/>
You can also use wrap_content
for height & width in ImageView.
So i came up with a workable solution. I have a layer-list drawable in /drawable
and /drawable-v21
.The layer-list drawable in /drawable-v21
contains a vector drawable. The layer-list drawable in /drawable
contains a .png . It kind of defeats the purpose of using vector drawables but I had to do this to make it work in this one off case.