How to set drawable size in layer-list

那年仲夏 提交于 2019-12-03 05:22:07

You can use android:width and android:height parameters with desired values.

Below is the sample drawable layer-list and screen shot. I used the vector drawable from google material icons and own selector drawable.

Original circle.xml drawable shape have 100dp x 100dp size, but into drawable.xml I override this values to 24dp x 24dp by android:width and android:height parameters.

I hope this help!

drawable.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/circle" android:height="24dp" android:width="24dp"/>
    <item android:drawable="@drawable/ic_accessibility_black_24dp"/>
    <item android:drawable="@drawable/ic_http_white_24dp" android:gravity="center" android:top="-3dp" android:height="5dp" android:width="5dp"/>
</layer-list>

circle.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:innerRadius="20dp" android:shape="oval">
            <solid android:color="#CCC" />
            <size android:width="100dp" android:height="100dp"/>
        </shape>
    </item>
</selector>

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.

Hope this will help :-
You can use a nine patch drawable for menu_drawable2. You can make menu_drawable2 like having transparent region around it edges. Use this transparent region to specify stretchable region in nine patch.

I also came across the issue on Android 5.0 (API 21). It seems the gravity of item doesn't work for API less than 23. So the layout is required to be updated, I've made it compatible by applying calculated paddings for the second item node which is supposed to be a right-top aligned overlay.

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

    <item
        android:left="16dp"
        android:right="0dp"
        android:top="0dp"
        android:bottom="16dp"
        android:drawable="@drawable/ic_circle_dot_red"
    />
</layer-list>

i dont know its works or not but did you tried to seperate them from each other?

something like this:

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

or put something else betwen them like empty layout with height and width 0dp.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!