初始屏幕无法正常工作-Xamarin

时间:2018-07-21 23:04:11

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

首先显示启动屏幕,然后淡出为黑色,然后显示该屏幕...

enter image description here

最后,它导航到应用程序的主屏幕。

这不令人满意。

如何摆脱黑色和上面的空白屏幕?

我的代码如下:

  
      
  1. 启动画面活动
  2.   
[Activity(Label = "App", MainLauncher = true, NoHistory = true, Theme = "@style/Splash",
            ConfigurationChanges = ConfigChanges.ScreenSize)]
public class SplashActivity : Activity
{
    protected override void OnResume()
    {
        base.OnResume();

        var startupWork = new Task(() =>
        StartActivity(new Intent(Application.Context, typeof(MainActivity))));

        startupWork.Start();
    }
}
  
      
  1. styles.xml:
  2.   
<style name="Splash" parent ="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowBackground">@drawable/splash</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowFullscreen">true</item>
</style>
  
      
  1. splash_screen.xml
  2.   
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <color android:color="@color/SplashBackground" />
    </item>
    <item>
        <bitmap
            android:src="@drawable/icon"
            android:tileMode="disabled"
            android:gravity="center" />
    </item>
</layer-list>

2 个答案:

答案 0 :(得分:1)

您可以执行3个步骤:

    您的MainActivity.cs中的
  1. MainLauncher = false,
  2. 在AppName.Android项目中创建一个类 (例如:MySplashScreen.cs)
  3. MySplashScreen.cs中的
  4. 与此代码相同。 (MainLauncher必须为真)
[Activity(Label = "ApplicationName",
        Theme = "@style/Theme.Splash",
        MainLauncher =true,
        NoHistory =true
        ),]
    public class MySplashScreen : Activity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Create your application here
            StartActivity(typeof(MainActivity));
        }
    }

答案 1 :(得分:0)

“将我的评论转换为答案...”

将包含windowBackground的主题应用于MainActivity,并且不要使用单独的“闪屏”活动,因为您对该活动不做任何事情

示例主题(只是一个子集):

<style name="Theme.ASushiApp.Main" parent="Theme.ASushiApp.Base">
  <item name="android:windowTranslucentStatus" tools:targetApi="kitkat">true</item>
  <item name="android:windowBackground">@drawable/splash_screen_layer</item>
</style>

MainActvity的布局中,将根ViewGroup的可见性设置为invisible。

或者您可以将背景设置为@android:color/transparent,并添加不确定的ProgressBar以显示旋转进度轮。

这里的目标是允许您在设置应用程序时继续显示主题背景图片。

然后在OnCreate上执行UI线程上必须要做的最低工作,然后启动Task

protected override void OnCreate(Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);
    SetContentView(Resource.Layout.Main);
    ~~~
    // setup toolbar/actionbar, FindViewById assignments, etc..
    ~~~
    new Task(InitalSetup).Start();
}

在后台,完成其余的设置,最后,在UI /主线程中,隐藏进度指示器或将根容器ViewGroup设置为可见,并将背景(Window.SetBackgroundDrawableResource)设置为纯色以清除您在主题中使用的图像。

async void InitalSetup()
{
    // running on a background thread...

    // setup db, any adapters, listeners, AdView handlers, etc... 
    // everything but direct UI changes, do those at the end on the UI/Main thread
    ~~~

    RunOnUiThread(() =>
    {
        // finish setup on the UI items that have to run on UI thread, ie: adapter assignments
        recyclerView.SetAdapter(adapter);

        rootViewGroupContainer.Visibility = ViewStates.Visible;
        // Or
        progressBar.Visibility = ViewStates.Gone;

        Window.SetBackgroundDrawableResource(Android.Resource.Color.Black);
    });