【发布时间】:2020-05-04 15:44:13
【问题描述】:
这个问题已在 SO 和 xamarin.forums 上多次提出。但我没能解决这个问题。
在我的 xamarin.forms android 应用程序中,我有一个启动画面。它可以完美启动。问题是在加载我的主页和启动屏幕之间,会出现一闪而过的白屏。我按照https://xamarinhelp.com/creating-splash-screen-xamarin-forms/ 的教程进行操作。
我尝试通过为启动画面创建特定活动并将启动主题直接设置为 MainActivity。在这两种方法中我都无法摆脱白屏闪烁。我在发布模式下尝试过,问题仍然存在。这是使用 xamarin 的预期行为吗?我怎样才能删除它?感谢您提供任何帮助。
我的 MainActivity
[Activity(Label = "SampleApp", Icon = "@mipmap/ic_launcher", Theme = "@style/splashscreen", ScreenOrientation = ScreenOrientation.Portrait, MainLauncher = true, LaunchMode = LaunchMode.SingleTop, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
static readonly string TAG = "MainActivity";
protected override void OnCreate(Bundle savedInstanceState)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
global::Xamarin.Forms.Forms.SetFlags("CollectionView_Experimental");
base.SetTheme(Resource.Style.MainTheme);
base.OnCreate(savedInstanceState);
}
}
我在 MainActivity 中有几个库 Init(),这里不包括在内。
我的 App.Xaml.cs
public partial class App : Application
{
public App()
{
var Login = new NavigationPage(new LoginPage());
MainPage = Login
}
protected override void OnStart()
{
}
protected override void OnSleep()
{
// Handle when your app sleeps
}
protected override void OnResume()
{
// Handle when your app resumes
}
}
我的风格
<style name="splashscreen" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowBackground">@drawable/Splash</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>
<style name="MainTheme" parent="MainTheme.Base">
</style>
<!-- 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>
<item name="android:backgroundDimEnabled">true</item>
<!-- colorAccent is used as the default value for colorControlActivated
which is used to tint widgets -->
<item name="colorAccent">#FF4081</item>
<item name="android:textAllCaps">false</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="AppCompatDialogStyle" parent="Theme.AppCompat.Light.Dialog">
<item name="colorAccent">#FF4081</item>
</style>
我的drawable/Splash
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<color android:color="@color/splash_background"/>
</item>
<item>
<bitmap
android:src="@drawable/Logo"
android:tileMode="disabled"
android:gravity="center"/>
</item>
</layer-list>
【问题讨论】:
-
你能分享一个带有闪烁的gif/视频吗?我过去也遵循过本教程,但没有看到任何闪烁。另外,能分享一下闪屏代码吗?
-
@MihailDuchev 嗨,我没有使用闪屏活动。我只是在 mainactvity 中设置启动主题并使用 base.SetTheme(Resource.Style.MainTheme); 将主题替换为 MainTheme;
-
@MihailDuchev 早些时候我尝试过使用 Splash 活动,但它对这个问题没有太大影响
-
你应该在这里添加
splashscreen样式 -
@Prateek 我添加了样式
标签: xamarin xamarin.forms xamarin.android