如何为MAUI框架的MauiSplashScreen添加渐变背景?
MauiSplashScreen 渐变背景实现方案
MauiSplashScreen 的 .csproj 配置确实不支持直接设置渐变背景,你可以通过以下两种方案实现需求:
方案一:生成带渐变背景的静态资源(跨平台通用,最简单)
用设计工具(Figma/Photoshop/Canva等)创建包含渐变背景+你的SVG前景的图片(保存为PNG或SVG),直接将这个图片作为启动屏源文件配置:
修改项目 .csproj 文件中的 MauiSplashScreen 节点:
<MauiSplashScreen Include="Resources\Images\splash_with_gradient.png" Resize="false" />
这种方式无需修改平台代码,各平台编译时会直接使用带渐变的完整图片,效果一致。
方案二:分平台自定义原生启动屏(更灵活)
MAUI 的启动屏本质是各平台原生组件,可通过修改各平台的原生配置实现渐变背景:
Android 平台
- 在
Platforms/Android/Resources/drawable目录下创建splash_screen.xml文件:
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 渐变背景层 --> <item> <shape> <gradient android:startColor="#808080" android:endColor="#000080" android:angle="45" /> <!-- 角度控制渐变方向 --> </shape> </item> <!-- 前景图标层 --> <item> <bitmap android:src="@drawable/hikari_white_rings" android:gravity="center" /> </item> </layer-list>
- 修改
Platforms/Android/Resources/values/styles.xml中的启动屏样式:
<style name="MainTheme.Splash" parent="Theme.Maui.SplashScreen"> <item name="android:windowBackground">@drawable/splash_screen</item> </style>
- 在
Platforms/Android/AndroidManifest.xml中确保启动 Activity 使用该样式:
<activity android:name=".MainActivity" android:exported="true" android:theme="@style/MainTheme.Splash">
iOS 平台
- 打开
Platforms/iOS/Resources/LaunchScreen.storyboard - 在可视化编辑器中:
- 添加一个
Gradient View作为根视图的背景,设置渐变颜色为#808080到#000080,调整方向 - 添加一个
ImageView,设置图片为hikari_white_rings,并设置居中对齐
- 添加一个
- 保存后,iOS 会自动使用这个故事板作为启动屏
Windows 平台
- 在
Platforms/Windows目录下创建SplashScreen.xaml文件:
<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Background="Transparent"> <Grid.Background> <LinearGradientBrush StartPoint="0,1" EndPoint="1,0"> <GradientStop Color="#808080" Offset="0"/> <GradientStop Color="#000080" Offset="1"/> </LinearGradientBrush> </Grid.Background> <Image Source="ms-appx:///Resources/Images/hikari_white_rings.svg" HorizontalAlignment="Center" VerticalAlignment="Center"/> </Grid>
- 修改
Platforms/Windows/Package.appxmanifest中的启动屏配置:
<uap:SplashScreen Image="SplashScreen.xaml" />
内容的提问来源于stack exchange,提问作者LordAhune
相关产品推荐
相关产品推荐

