我创建了一个闪屏使用这篇文章从MS:
https://learn.microsoft.com/en-us/xamarin/android/user-interface/splash-screen
问题是,当应用程序启动时:
1.它以红色背景开始
1.然后加载闪屏图像
1.然后加载MainActivity
我如何摆脱第一步?我想直接显示闪屏。
在MainActivity中,我设置了MainLauncher = false和Theme =“@style/MainTheme”:
[Activity(Label = "S", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = false, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
splash_screen.xml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" android:opacity="opaque">
<item>
<color android:color="@color/splash_background"/>
</item>
<item>
<bitmap
android:src="@drawable/splash_logo"
android:tileMode="disabled"
/><!--deleted android:gravity="center" to get a fullscreen image-->
</item>
</layer-list>
style.xml:
<?xml version="1.0" encoding="utf-8" ?>
<resources>
<!-- Base theme applied no matter what API -->
<style name="MainTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
<!--If you are using revision 22.1 please use just windowNoTitle. Without android:-->
<item name="windowNoTitle">true</item>
<!--We will be using the toolbar so no need to show ActionBar-->
<item name="windowActionBar">false</item>
<!-- Set theme colors from http://www.google.com/design/spec/style/color.html#color-color-palette -->
<!-- colorPrimary is used for the default action bar background -->
<item name="colorPrimary">#2196F3</item>
<!-- colorPrimaryDark is used for the status bar -->
<item name="colorPrimaryDark">#1976D2</item>
<!-- colorAccent is used as the default value for colorControlActivated
which is used to tint widgets -->
<item name="colorAccent">#FF4081</item>
<!-- You can also set colorControlNormal, colorControlActivated
colorControlHighlight and colorSwitchThumbNormal. -->
<item name="windowActionModeOverlay">true</item>
<item name="android:datePickerDialogTheme">@style/AppCompatDialogStyle</item>
</style>
<style name="MainTheme.Splash" parent ="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowBackground">@drawable/splash_screen</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowActionBar">true</item>
</style>
<style name="MainTheme" parent="MainTheme.Base">
</style>
<style name="AppCompatDialogStyle" parent="Theme.AppCompat.Light.Dialog">
<item name="colorAccent">#FF4081</item>
</style>
</resources>
SplashActivity.cs:
[Activity(Theme = "@style/MainTheme.Splash", MainLauncher = true, NoHistory = true, ScreenOrientation = ScreenOrientation.Portrait) ]
public class SplashActivity : AppCompatActivity
{
static readonly string TAG = "X:" + typeof(SplashActivity).Name;
public override void OnCreate(Bundle savedInstanceState, PersistableBundle persistentState)
{
base.OnCreate(savedInstanceState, persistentState);
}
// Launches the startup task
protected override void OnResume()
{
base.OnResume();
Task startupWork = new Task(() => { SimulateStartup(); });
startupWork.Start();
SimulateStartup();
}
// Simulates background work that happens behind the splash screen
async void SimulateStartup()
{
//Log.Debug(TAG, "Performing some startup work that takes a bit of time.");
await Task.Delay(5000); // Simulate a bit of startup work.
//Log.Debug(TAG, "Startup work is finished - starting MainActivity.");
StartActivity(new Intent(Application.Context, typeof(MainActivity)));
}
public override void OnBackPressed() { }
}
}
1条答案
按热度按时间gajydyqb1#
在Android 12中更改了闪屏要求。推荐的方式已经改变。以下是在现代Xamarin.Android应用中如何做到这一点:
1.向
Xamarin.AndroidX.Core.SplashScreen
添加NuGet引用1.在活动OnCreate中使用新的SplashScreen API安装启动画面。
1.创建一个保持在屏幕上的条件,以确定启动画面何时应该消失。
1.在
[Activity]
或AndroidManifest.xml中更新主题。MainActivity.cs:
TrueKeepOnScreenCondition.cs:
我总是返回true,因为我使用的是MvvmCross,当MvvmCross完成加载时,有一个单独的进程将完成splash活动。
AndroidManifest.xml:
Resources\values\themes.xml: