What's mipmap-anydpi-v26 in res directory in Android Studio 3.0?

会有一股神秘感。 提交于 2019-11-28 23:43:48

问题


In Android Studio 3.0, once we create a project, a folder named mipmap-anydpi-v26 is automatically created in the res directory. What actually does it do? Why do we need it? How will we utilize it for development purposes?

Also, there are two XML files automatically created in this folder after project setup. Why do these XML files reside in a mipmap folder? I thought we should keep all XML files in a drawable folder instead of mipmap.


回答1:


Android Studio 3 creates an adaptive icon for your app which is only available in SDK 26 and up. Launcher icons should be put into the mipmap folders.

If you look at your manifest, you can see that it references ic_launcher

android:icon="@mipmap/ic_launcher"

If you look in your mipmap folder, you see your normal 5 different launcher icons which will be used for anything lower than SDK 26. For SDK 26 and up, it uses the XML files from the anydpi-v26 folder to make use of adaptive icon.

<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
    <background android:drawable="@drawable/ic_launcher_background"/>
    <foreground android:drawable="@mipmap/ic_launcher_foreground"/>
</adaptive-icon>



回答2:


Both answers above give pretty good summary of what mipmap-anydpi-v26 folder does but I feel why part is missing. So heres my 2 cents.

anydpi: These resources take precedence in any dpi. So even if you have mipmap-hdpi or mipmap-mdpi matching with current devices density, resource from mipmap-anydpi will always be picked up.

anydpi-v26: This is an additional filter over just anydpi. This says resources will always be picked up from anydpi-v26 regardless of devices density only if SDK/API level is 26 or higher(Oreo).

So you can have mipmap-anydpi-v26 or drawable-anydpi-v26. All resource folders will follow above logic.

Now that we know the answer of "why mipmap-anydpi-v26"? lets try to understand why "mipmap-anydpi-v26/ic_launcher.xml".

This is because ic_launcher.xml is used to describe adaptive icon for your app which is only available in SDK 26 and up as mentioned by other answers. Hope this helps.




回答3:


I have found an explanation about this, here is some context:

To add an adaptive icon that replaces all PNGs on API 26+ devices, you’ll add a res/mipmap-anydpi-v26/ic_launcher.xml file that looks like this:

<adaptive-icon
    xmlns:android="http://schemas.android.com/apk/res/android">
  <background android:drawable="@drawable/ic_launcher_background"/>
  <foreground android:drawable="@drawable/ic_launcher_foreground"/>
</adaptive-icon>

By placing it in the mipmap-anydpi-v26 folder, the resource system will use it in preference over any files in the other dpi folders (exactly what you want, since this file is replacing all of them) and that it should only be used in API 26+ devices.



来源:https://stackoverflow.com/questions/45080712/whats-mipmap-anydpi-v26-in-res-directory-in-android-studio-3-0

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