Android Studio 1.4:在使用“File/New/Vector Asset”选项时忽略drawable-v21

时间:2022-11-21 17:28:57

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.

我通过Android Studio 1.4的新选项——new /Vector Asset导入了一些图标。正如您所知道的,它允许您轻松地导入和使用向量绘制,并创建与api <21兼容的pngs。它将pngs放入文件夹app/build/generate /res/pngs/debug/drawable- (mdpi、ldpi、hdpi等)。它还生成所有文件夹的-v21版本,并将vector drawable .xmls放在那里。

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:

当我想给其中一个图标添加波纹效果时,问题就出现了。我把....无裂版本进入我的res/drawable文件夹。它看起来像这样:

drawable/ic_settings.xml

可拉的/ 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:

然后我想把ripple添加到API >21中的图标中。因此,在res/drawable-v21文件夹中,我添加了这个drawable:

drawable-v21/ic_settings.xml

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-v21automatically generated based on the file in res/drawable. So no ripple.

现在,当我在API 21下运行这个应用程序时,它不是使用来自drawable-v21的资源,而是使用来自app/build/generated/res/pngs/debug/drawable-xxhdpi-v21中的一个drawable。所以没有涟漪。

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

当我试图从res/drawable/中删除无裂口的文件时,涟漪又回到了API 21中,但是当然这个应用程序在API <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.

然后我试着将这个图标的纹波版本放到res/drawable文件夹中,希望gradle能够自动生成png。不- 标签阻止gradle生成pngs,程序崩溃<21。

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

有人遇到过这种问题吗?你知道如何在图标中加入ripple吗?

Have a good day!

有一个美好的一天!

1 个解决方案

#1


1  

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-v21and duplicated my layout, but instead of ic_settings.xml i used ic_settings_ripple.xml.

在文件夹res/drawable中,我只留下了能够和应该自动转换为png的向量——所以只有普通的普通图标没有波纹。然后在drawable_v21中,我将drawable的ripple版本重新命名为ic_settings_ripple.xml。我已经创建了一个新的文件夹layout-v21,并复制了我的布局,而不是ic_settings。我用ic_settings_ripple.xml 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.

因此,现在在API<21下,应用程序使用的是由gradle生成的ripple-less png,而在API>=21中,有一个替代的布局,它使用了一个图标的波纹版本。

#1


1  

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-v21and duplicated my layout, but instead of ic_settings.xml i used ic_settings_ripple.xml.

在文件夹res/drawable中,我只留下了能够和应该自动转换为png的向量——所以只有普通的普通图标没有波纹。然后在drawable_v21中,我将drawable的ripple版本重新命名为ic_settings_ripple.xml。我已经创建了一个新的文件夹layout-v21,并复制了我的布局,而不是ic_settings。我用ic_settings_ripple.xml 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.

因此,现在在API<21下,应用程序使用的是由gradle生成的ripple-less png,而在API>=21中,有一个替代的布局,它使用了一个图标的波纹版本。