隐藏

Xamarin.Forms Android App添加Splash启动画面

发布:2021/11/3 17:34:00作者:管理员 来源:本站 浏览次数:980

Android App启动的时候,特别是基于Xamarin.Foms的App启动的时候总是出现一个白屏要等一会,这样的情况加一个Splash画面是最理想的了,画面可以是广告、软件口号等。

折腾很久,简化过程如下:


   弄一个png图片(主要是可以支持底色透明,可以弄得很漂亮,其实别的图片格式也可以,分辨率自己看着办。),添加到Resources\drawable文件夹里,这里统一叫splashscreen.png,如果不叫这个文件名,下面的代码里的相关位置记得改名。

   打开Resources\values\styles.xml,在里面加一段:


<!--以下为Splash屏幕而添加-->

 <style name="Theme.Splash" parent="android:Theme.Holo.Light">

   <item name="android:windowBackground">@drawable/splashscreen</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>

<!--达叔傻乐 (darwin.zuo@163.com)-->


   可以新建一个类来放下面的代码(我这里把它放到MainActivity.cs文件里跟MainActivity一起了)。打开MainActivity.cs文件,加入下面的代码:


<!--以下为Splash屏幕而添加-->

[Activity(MainLauncher = true, NoHistory = true, Theme = "@style/Theme.Splash",

ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]

   public class SplashScreen : Activity

   {

       protected override void OnCreate(Bundle bundle)

       {

           base.OnCreate(bundle);

           var intent = new Intent(this, typeof(MainActivity));

           StartActivity(intent);

           Finish();

       }

   }

<!--达叔傻乐 (darwin.zuo@163.com)-->


   记得把原来的MainActivity类的 MainLauncher 属性改为 false。