VectorDrawable: Android loads xhdpi PNG's inst

2019-04-28 01:03发布

I'm trying to use a VectorDrawable on API21, but Android loads the PNG resource from xxhdpi folder instead. My current res structure as follows:

  • res
    • drawable-xxhdpi
      • test_icon.png
    • drawable-21
      • test_icon.xml

And my XML layout:

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/test_icon"/>

Are there any other ways to solve this? From my understanding Android will always pick the PNG resource, but if that's the case, how one can use VectorDrawables for API21 and PNG for lower API's?

[Update 1]

If we use a drawable-xxhdpi-21 resource folder, Android will pick the vector instead of the PNG resource. But that means we would have to have a copy (or symlink) of the file for other densities as well (e.g. xhdpi, hdpi, etc)

2条回答
狗以群分
2楼-- · 2019-04-28 01:25

drawable-anydpi-v21 is right for that

drawable-anydpi-v21/test_icon.xml

https://google.github.io/material-design-icons/#icons-for-android

查看更多
仙女界的扛把子
3楼-- · 2019-04-28 01:35

The solution I found is a bit of a hack but seems to work. Instead of putting your VertorDrawable xml files in each of the bucket with the version, if you put them in drawable-w1dp-v21 it takes the right resource and don't need to have multiple copies.

This is because, when deciding which resource to use, Android checks the density and after the minimum width density, which in this case is always gonna be more than 1dp :)

Update: as @pengrad mentions best to use the anydpi bucket. :)

查看更多
登录 后发表回答