Xamarin : Splash screen using a Layout

2019-08-08 15:57发布

I am trying to create splash screen for my android application, as shown in this link http://developer.xamarin.com/guides/android/user_interface/creating_a_splash_screen/

Unfortunately this link just shows how to make a splash screen using a drawable. But what I need to do is to create a splash screen using a Layout so that I can easily customize how it looks and make it compatible with different screen sizes.

Thanks

1条回答
放我归山
2楼-- · 2019-08-08 16:42

What you can do is to create a Activity that represents your splash screen. Ex: SplashActivity. Then when your SplashActivity is created you start a timer (Ex: System.Timers.Timer) with 3 seconds duration. When those 3 seconds have passed you simply start the main activity for your app.

To prevent the user from navigating back to the splash activity, you simply add the NoHistory = true property to the ActivityAttribute (just above the activity class decleration).

See example:

    [Activity(MainLauncher = true, NoHistory = true, Label = "My splash app", Icon = "@drawable/icon")]
    public class SplashActivity : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.Splash);

            Timer timer = new Timer();
            timer.Interval = 3000; // 3 sec.
            timer.AutoReset = false; // Do not reset the timer after it's elapsed
            timer.Elapsed += (object sender, ElapsedEventArgs e) =>
            {
                StartActivity(typeof(MainActivity));
            };
            timer.Start();
        }
    };

    [Activity (Label = "Main activity")]
    public class MainActivity : Activity
    {
        protected override void OnCreate (Bundle bundle)
        {
            base.OnCreate (bundle);

            // Set our view from the "main" layout resource
            SetContentView (Resource.Layout.Main);
        }
    }
查看更多
登录 后发表回答