首页 > 解决方案 > Xamarin Forms - Android Splashscreen 也显示在主应用程序中

问题描述

我在我的应用程序中添加了一个启动画面

<item name="android:windowBackground">@drawable/splashScreen</item>

MainTheme我的应用程序。

启动画面在应用程序启动时正确显示,但在应用程序的所有页面中仍然作为背景。

我怎样才能避免这种行为?

这里有一个视频可以更好地展示这种行为。

谢谢!

标签: androidxamarin.formsxamarin.androidsplash-screen

解决方案


您需要至少有两个主题。一个用于启动屏幕,另一个用于您的应用程序的其余部分(您也可以为特定页面设置其他主题)。

在我的例子中,我有“splashscreen”用于启动屏幕,“AppTheme”用于应用程序的其余部分。

将启动主题定义为:

<style name="splashscreen" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowBackground">@drawable/Zenon_Welcome_Screen</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowIsTranslucent">false</item>
    <item name="android:windowIsFloating">false</item>
    <item name="android:backgroundDimEnabled">true</item>
    </style>

和 AppTheme 为:

<style name="AppTheme" parent="AppTheme.Base">
  </style>
  <!-- App theme applied no matter what API -->
  <style name="AppTheme.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">#c62828</item>
    <!-- colorPrimaryDark is used for the status bar -->
    <item name="colorPrimaryDark">#8e0000</item>
    <!-- colorAccent is used as the default value for colorControlActivated
         which is used to tint widgets -->
    <item name="colorAccent">#8e0000</item>
    <item name="windowActionModeOverlay">true</item>
    <item name="android:datePickerDialogTheme">@style/AppCompatDialogStyle</item>
  </style>

现在在您的主要活动中:

[Activity(Label = "AppName", Icon = "@drawable/icon", Theme = "@style/splashscreen", MainLauncher = true,
              ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation, ScreenOrientation = ScreenOrientation.Portrait,
        LaunchMode = LaunchMode.SingleTask)]
    public class MainActivity : FormsAppCompatActivity

现在在 onCreate 方法中,您需要切换主题,如下所示:

        /// <summary>
        ///    On create.
        /// </summary>
        /// <param name="bundle">Bundle.</param>
        protected override void OnCreate(Bundle bundle)
        {
            base.Window.RequestFeature(WindowFeatures.ActionBar);

            // Name of the MainActivity theme we want to use for the app.
            // Or we can use global::Android.Resource.Style.ThemeHoloLight / Theme Name.
            base.SetTheme(Resource.Style.AppTheme);

            base.OnCreate(bundle);
        }

推荐阅读