MAUI .NET 8 Android端SplashScreen Duration设置10秒无效求助
解决MAUI Android闪屏无法停留10秒的问题
你的问题出在MAUI的<Duration>属性在Android平台上不生效——Android系统的闪屏机制是应用初始化完成后立即切换到主界面,MAUI的这个配置只对iOS等其他平台的动画过渡有效,无法强制Android系统延长闪屏停留时间。
要实现固定10秒的闪屏,需要通过Android原生自定义启动Activity来控制,步骤如下:
1. 创建自定义闪屏Activity
在Android项目(YourAppName.Droid)中添加SplashActivity.cs:
using Android.App; using Android.Content; using Android.OS; namespace YourAppName.Droid; [Activity(Theme = "@style/SplashTheme", MainLauncher = true, NoHistory = true)] public class SplashActivity : Activity { protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); // 延迟10秒跳转至MAUI主页面 new Handler(Looper.MainLooper).PostDelayed(() => { StartActivity(new Intent(this, typeof(MainActivity))); Finish(); }, 10000); // 10000毫秒 = 10秒 } }
2. 修改MainActivity的启动配置
打开MainActivity.cs,移除MainLauncher = true(把启动权交给SplashActivity):
[Activity(Theme = "@style/Maui.SplashTheme", LaunchMode = LaunchMode.SingleTop, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)] public class MainActivity : MauiAppCompatActivity { }
3. 添加闪屏主题
在Android项目的Resources/values/styles.xml中添加闪屏主题:
<style name="SplashTheme" parent="Theme.MaterialComponents.Light.NoActionBar"> <item name="android:windowBackground">@drawable/splash_screen</item> <item name="android:windowFullscreen">true</item> <item name="android:windowContentOverlay">@null</item> </style>
4. 配置闪屏背景资源
在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 android:drawable="@color/white" /> <item> <bitmap android:gravity="center" android:src="@mipmap/splash" /> <!-- MAUI会自动生成该图片资源 --> </item> </layer-list>
5. 保留MAUI闪屏配置(可选)
你原来的<MauiSplashScreen>配置可以保留,它会自动生成不同分辨率的图片资源,适配各种Android设备:
<MauiSplashScreen Include="Resources\Images\splash.png"> <Duration>10000</Duration> <!-- 仅对iOS等平台生效,Android无需依赖 --> </MauiSplashScreen>
完成以上配置后,重新编译部署到Android设备,闪屏就会稳定停留10秒后跳转至首页。
内容的提问来源于stack exchange,提问作者BulentBilir
相关产品推荐
相关产品推荐

