Android Layer-List VectorDrawable size
Asked Answered
V

1

11

My question is related to this one: link. I've recently changed most of my resources for Android from different png files for different resolutions to vector drawables. I'm using a "swipe-to-delete" library that can change the background of its views during the swipe to show a Gmail-style red background with a trashcan icon, something similar to this (which has a green background with a check icon) :

enter image description here

I used to do that using the following code, and I still do for < API 21

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <color android:color="@color/accent"/>
    </item>
    <item android:right="@dimen/activity_horizontal_margin">
        <bitmap
            android:gravity="right|center_vertical"
            android:src="@drawable/ic_delete_24dp"/>
    </item>
</layer-list>

However, different syntax is required for API 21+

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <color android:color="@color/accent" />
    </item>
    <item android:right="@dimen/activity_horizontal_margin"
          android:drawable="@drawable/ic_delete_24dp"
          android:gravity="right|center_vertical"/>
</layer-list>

Now, instead of an icon on the left/right hand side, the icon on API 21 & API 22 (Lollipop) is stretched to fill the screen. Is there any way to specify the width of the icon?

Vicious answered 6/11, 2015 at 22:41 Comment(2)
Just as an update, this bug is fixed past API 23 (Marshmallow), and only exists on LollipopVicious
The property android:drawable gives crash in API 19 and app:srcCompat doesnt seem to work in layer-list. How are you handling it?Photoneutron
S
0

Add this to your app level build.gradle file

defaultConfig {
     vectorDrawables.useSupportLibrary = true
}
Sill answered 23/2, 2018 at 5:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.