android - Xamarin Forms - Android Splashscreen 也显示在主应用程序中

标签 android xamarin.forms xamarin.android splash-screen

我已经在我的应用程序中添加了启动画面

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

到我的应用程序的 MainTheme

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

如何避免这种行为?

Here更好地展示行为的视频。

谢谢!

最佳答案

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

在我的例子中,我有启动画面的“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 中:

[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);
        }

关于android - Xamarin Forms - Android Splashscreen 也显示在主应用程序中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55182033/

相关文章:

java - E/RecyclerView : No adapter attached; skipping layout while trying to add TextView to layout

c# - 在 Xamarin.Forms 中,如何从项目文件夹中读取 Assets 列表?

c# - Xamarin Forms SQLite 更新

java - C# 或 Java(在 android 上的性能)

android - Jetpack 与 RxJava2 和 Realm 组合

android - 如何在Android中控制EditText中的输入?

java - 创建接听电话通知

android - Xamarin Forms - 如何检查 Android 上 RequestIgnoreBatteryOptimizations 权限的设置

c# - 在 Xamarin.forms 中检测到的目标依赖项中的循环

android - Xamarin.Android 绑定(bind)使用 EnumMethods.xml 定义 Getter/Setter 方法作为接口(interface)