How to set drawable size in layer-list

Deimos picture Deimos · Aug 18, 2011 · Viewed 24.7k times · Source

I'm having problem with setting size of drawable in layer-list. My XML looks like this:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:drawable="@drawable/menu_drawable1"/>
    <item android:drawable="@drawable/menu_drawable2"/>       
</layer-list>

Drawable1 is bigger than Drawable2, so Drawable2 gets resized. How to override this and set the original size of Drawable2? I don't want it to resize. I tried with setting width and height but that doesn't work. Also the drawables aren't actually bitmaps, they are selectos, so I can't use the bitmap trick.


It looks like I've got half of the solution by using "left" and "right", but still, this doesn't let me set the exact size of the image automatically. Now my xml looks like this:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:drawable="@drawable/menu_drawable1"/>
    <item android:drawable="@drawable/menu_drawable2" android:right="150dp"/>       
</layer-list>

Answer

Andrii Senkiv picture Andrii Senkiv · May 23, 2016

You can use inset tag for this. For example

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:drawable="@drawable/menu_drawable1"/>
   <item>
       <inset android:drawable="@drawable/menu_drawable2"
           android:insetRight="50dp"
           android:insetTop="50dp"
           android:insetLeft="50dp"
           android:insetBottom="50dp" />
   </item>

This will create paddings around you menu_drawable2 so it won`t be scaled. Just set the dimensions that you require.