简体   繁体   中英

Android Studio 1.4: drawable-v21 ignored when using “File/New/Vector Asset” option

This is my first SO question so please don't eat me alive... Here's my problem:

I've imported few icons via Android Studio 1.4's new option - New/Vector Asset . As you know it lets you easily import and use vector drawables and creates pngs for compatibility with APIs <21. It puts the pngs in folders app/build/generated/res/pngs/debug/drawable-<density> (mdpi, ldpi, hdpi etc). It also generates the -v21 versions of all the folders and puts the vector drawable .xmls there.

The problem started, when I wanted to add ripple effect to one of the icons. I've put the .... ripple-less version into my res/drawable folder. It looked like this:

drawable/ic_settings.xml

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24dp"
    android:height="24dp"
    android:viewportWidth="24.0"
    android:viewportHeight="24.0">
<path
    android:fillColor="#FFFFFF"
    android:pathData="M12,15.5A3.5,3.5 ...blablabla notimportatnt"/>
</vector>

Then I wanted to add ripple to the icon in API >21. So to the folder res/drawable-v21 I added this drawable:

drawable-v21/ic_settings.xml

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
     android:color="?android:colorControlHighlight">
<item>
    <vector
        android:width="24dp"
        android:height="24dp"
        android:viewportWidth="24.0"
        android:viewportHeight="24.0">
        <path
            android:fillColor="#FFFFFF"
            android:pathData="M12,15.5A3.5,3.5 0,0....blah blah blah"/>
    </vector>
</item>
</ripple>

Now, when I run the app under API 21, instead of using resources from drawable-v21 it uses a drawable from app/build/generated/res/pngs/debug/drawable-xxhdpi-v21 automatically generated based on the file in res/drawable . So no ripple.

When I tried to remove the ripple-less file from res/drawable/ , the ripple comes back in API 21, but of course the app crashes in APIs<21

Then I tried to put ripple version of the icon to the res/drawable folder, hoping that gradle would automatically generate the pngs. Nope - <ripple> tag prevents gradle from generating pngs and the app crashes <21.

Anyone stumbled upon this kind of problem? Any idea how to add ripple to the icon?

Have a good day!

Ok, I kinda found the solution.

In folder res/drawable I left only the vectors that can and should be automatically converted to pngs - so only plain normal icons without ripple. Then in drawable_v21 I've put the ripple version of drawable and renamed it - ic_settings_ripple.xml . I've created a new folder layout-v21 and duplicated my layout, but instead of ic_settings.xml i used ic_settings_ripple.xml .

So now under API<21 the app uses ripple-less png generated by gradle, and at API>=21 there is an alternative layout, that uses ripple version of one of the icon.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM