使用XAML文件(Xamarin)为Android创建启动画面

时间:2019-05-15 13:34:40

标签: c# android xaml xamarin

我有一个项目,其中您从登录屏幕开始,该屏幕是在XAML的帮助下生成的。在此之前,我会加载一个保存我的初始屏幕的活动。当然,活动只能在我的SetContentView()方法中加载一个AXML文件,因此,初始屏幕是使用AXML生成的。

我想同时在登录和初始屏幕上放置两个图标。如果我希望将它们放在同一位置,以避免在将启动画面更改为登录屏幕时跳动,则很难准确定位它们。

我的问题是,是否还可以通过XAML加载初始屏幕?

最诚挚的问候,Manuela

1 个答案:

答案 0 :(得分:0)

根据您的描述,您希望在应用程序的开头有两个启动屏幕,对吗?如果是,我将提供一个样本供您查看:

首先,您需要创建三个活动,一个是MainActivity,另一个是Activity1,另一个是Activity2。

MainActivity:

[Activity(Label = "@string/app_name")]
public class MainActivity : Activity
{     
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.activity_main);         
    }
}

活动1:

[Activity(Theme = "@style/MyTheme.Splash", MainLauncher = true, NoHistory = true)]
public class Activity1 : Activity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        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(Activity2));
        };
        timer.Start();

    }
}

活动2:

[Activity(Theme = "@style/MyTheme.Splash1",NoHistory = true)]
public class Activity2 : Activity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        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();

    }
}

splash_screen.xml:

<?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/splash_logo"
    android:tileMode="disabled"
    android:gravity="center"/>
 </item>

splash_screen1.xml:

<?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/a9"
    android:tileMode="disabled"
    android:gravity="center"/>
 </item>

enter image description here