在Xamarin.Forms Android应用中未显示应用程序图标

4
我无法理解为什么我的Xamarin.Forms Android应用程序具有默认的机器人图标。
这是我现在的结构: enter image description here 以下是“MainActivity.cs”中的内容:
[Activity(Label = "My App Name", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity

这是我的 "AndroidManifest.xml" 文件中的内容:

<application android:label="My App Name" android:icon="@mipmap/icon"></application>

在 "icon.xml" 文件中:

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

我在网上找到一些解决方案建议将图标移动到“drawable”文件夹中,但我不确定这些解决方案是否最新。我使用的是Visual Studio 2019。


1
检查不同mipmap目录中的所有icon.png文件是否具有正确的图像。此外,如果您打开mipmap-anydpi-v26目录中的icon.xml文件,它是什么样子? - pinedax
我有完全相同的问题,我的代码看起来和你的一模一样。我会尝试将图标移动到.Android/Resources/drawable文件夹中并重新构建项目。 - Ola Ström
1个回答

5
mipmap-anydpi-v26是指对于SDK版本号大于等于26的设备,它使用XML文件夹中的文件来使用自适应图标。如果你的设备版本低于26,你需要检查icon.png是否正确存储在文件夹中。如果版本号大于等于26,则需要检查icon.xml。顺便说一下,最简单的方法是将你的icon.png放入Resources/drawable文件夹中,然后像这样使用:
[Activity(Label = "My App Name", Icon = "@drawable/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity

谢谢您的回答。您是否建议将icon.png放置到每个drawable文件夹中,即drawable、drawable-hdpi、drawable-xxhdpi、drawable-xxxhdpi,还是仅放置在drawable文件夹中?为什么这样做比让它们留在原地更容易呢?另外,如果我将它们移动到drawable文件夹中,是否应该从mipmaps中删除它们? - David Shochet
1
是的,它与mipmaps类似,也根据像素密度加载图像。顺便说一句,我想说mipmap文件夹只用于放置应用程序/启动器图标(这些在主屏幕上显示)。您使用的任何其他可绘制资产都应放置在相关的drawable文件夹中,就像以前一样,这也是官方建议。但现在,我不知道为什么您的图标没有显示,您是否检查了icon.xmlicon.png?或者尝试重命名它们,如果仍然无法正常工作,则将该图标放入drawable文件夹中作为解决方法。 - Leo Zhu

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