自适应图标无法正常工作

16

清单文件:

<application
    android:name="..."
    android:allowBackup="false"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/MyTheme"
    tools:replace="icon,label,theme,name,allowBackup">

在文件夹mipmap-anydpi-v26下,我定义了ic_launcher.xml

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

这是我的文件夹结构: 在此输入图片描述

build.gradle:

compileSdkVersion = 26
buildToolsVersion = "25.0.2"
supportLibVersion = "25.3.1"
targetSdkVersion = 25
minSdkVersion = 18

而且,我正在使用Android Studio 3.0

但最终结果是,我得到的是默认的Android图标,而不是我提供的那个图标。

我也尝试将前景PNG放入所有密度文件夹中(mipmap-xhdpi等),尽管我在测试时对所有密度都使用了相同的PNG。


你是在使用模拟器还是真实设备?如果是设备,是什么类型的? - Karol Kulbaka
使用模拟器,Nexus 5X 在 SDK 26 上。 - Siavash
2
很难说......你尝试更新构建工具了吗?另一件事是模拟器不能支持图标变化。 - Karol Kulbaka
1
@KarolKulbaka 这是构建工具版本的问题,如果您愿意,请将其提交为答案,否则我会。 - Siavash
你尝试将 android:roundIcon="@mipmap/ic_launcher_round" 添加到你的 AndroidManifest 文件了吗?看看这个自适应图标 - dotGitignore
请查看我在这里的回答。 - Aniket Thakur
5个回答

11

自适应图标需要API 26,因此您需要将构建工具更新至至少26.0.0版本。


谢谢,这非常有帮助。 - Marc
12
我已将构建工具设置为26.0.2,但仍显示默认图标。 - Etienne Lawlor
4
在Android Studio中,转到帮助->“查找操作...”,输入“图像资产”并单击打开。在那里,您可以指定前景图像、背景图像(或颜色),它将自动生成所有启动器图像和XML文件。 - Cristi
@toobsco42 我也遇到了同样的问题,但最终发现是因为我错误地使用了错误的XML定义(基本上我使用了Android Studio建议的自动修复,但在这种情况下这是个坏主意)。 - blunden
我在官方文档https://developer.android.com/guide/practices/ui_guidelines/icon_design_adaptive中没有看到构建工具的要求,这个在哪里有记录? - BluePie
好的,我想我明白了,对于其他有这个疑问的人,请参考 https://developer.android.com/studio/releases/build-tools.html。 - BluePie

5

我也遇到了同样的问题,这是我解决这个问题的方法:

  1. 右键点击资源 -> 新建 -> ImageAsset

  2. 按照下面屏幕显示的方式选择ic_launcher_background图标和ic_launcher_foreground

在此输入图片描述

  1. Android studio creates an ic_launcher.xml under resource mipmap (anydpi-v26)

     <?xml version="1.0" encoding="utf-8"?>
    <adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
        <background android:drawable="@mipmap/ic_launcher_background"/>
        <foreground android:drawable="@mipmap/ic_launcher_foreground"/>
    </adaptive-icon>
    
  2. Now inside the Manifest.XML, declare the icon and round icon something shown in like below

    <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    
    
                    .......</application>
    

    Yes that's all and Run your app on any device it appears


是的。我使用了 drawable/ic_launcher 而不是 mipmap/ic_launcher。这个用法并不常见,而且我认为文档也不是很好。 - methodsignature
有点是的,我们需要深入挖掘并详细了解。https://developer.android.com/guide/practices/ui_guidelines/icon_design_adaptive - Suresh Maidaragi
成功了。如果你替换了启动器图标,你需要记得替换mipmap的drawable。这个问题困扰了我好久。 - ZShock

2

我遇到了自适应图标无法显示的问题。事实证明我没有做错什么。在Android Studio中执行“Clean Project”之后,它开始正常工作。


我也一切都很好。我正准备尝试不同的配置。谢谢! - JCarlosR

0

我尝试使用一个<ImageView>来调试它。但是,这样做时,我得到了一个以以下内容结尾的回溯:

Caused by: java.lang.IllegalArgumentException: Path string cannot be empty.

原来我的 ic_launcher_foreground.xml 文件中有一些带有空的 android:pathData 属性的 <path> 元素。
删除这些空的 <path> 元素后,图标就可以正常工作了!

0

ic_launcher.xml 应该像这样

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接