初入门Xamarin,研究了下 安卓的 启动闪屏,网上查了一下,能参考的资料并不多,我参考的的以下博客 

http://blog.csdn.net/zapzqc/article/details/38496117

我按着以上作者的方法去 添加到最后发现 装上手机后 闪屏过后 APP就崩溃了,所以我深入研究了一下 发现问题所在以下为闪屏添加的详细一点的方法及过程中遇到的 问题 的解决方法 

源码及安装包下载:  下载地址

先来看下效果

工具: Visual Studio2017  

1、创建个 移动app Xamarin窗体

2、随便在 MainPage.xaml 中添加一点 控件和 文字  (也就是闪屏显示结束后进入的Main页面)
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:SplashScreen"
             x:Class="SplashScreen.MainPage">

    <StackLayout BackgroundColor="GreenYellow">
        <Label Text="闪屏结束   欢迎来到 主界面" FontSize="Large"></Label>
        <Label Text="闪屏结束   欢迎来到 主界面" FontSize="Large"></Label>
        <Label Text="闪屏结束   欢迎来到 主界面" FontSize="Large"></Label>
        <Label Text="闪屏结束   欢迎来到 主界面" FontSize="Large"></Label>        
    </StackLayout>

</ContentPage>

3、将要作为闪屏的图片 命名为  splash_screen.png ,然后拖到 Resources --> drawable 文件夹下(名字随意取,随意取的话要对应接下来的一步,支持PNG,BMP ,JPG等图片格式  )

4、在 Resources --> drawable 文件夹 右键 >添加>新建项>XML文件>名字改为 splashscreen.xml

splashscreen.xml 中添加 下面内容, 如果第3步你的闪屏图片名字不是 splash_screen的话 那就将

android:src="@drawable/splash_screen"   改为  android:src="@drawable/(闪屏图片的名字,不用加后缀)"  

可设置  android:gravity="fill" 和 android:layout_gravity="center" 这个来改变你 闪屏的 居中对齐方式

<?xml version="1.0" encoding="utf-8" ?>   
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"   
android:src="@drawable/splash_screen"   
android:gravity="fill"   
android:layout_gravity="center"/>  

5、在项目的Resources>values >styles.xml 文件中  <resources> </resources>添加 以下 几个新的样式,保留原有的,否则闪屏结束APP就崩溃,此样式是直接增加,不是删掉原有的 再添加,上文作者的可能用的是旧版本,所以不会出现问题,但VS2017中会崩溃,添加后保存

  <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>  

6、在SplashScreen.Android 下右键>添加>类>重命名为SplashScreen.cs  (SplashScreen.Android也就是 你 APP名.Android  这个项目名

首先再该类中引用 using Android.Content.PM;

然后在命名控件下 加入这句 (注意 不是在类里)

  1. [Activity(MainLauncher = true, NoHistory = true, Theme = "@style/Theme.Splash",  
  2.    ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)] 

7、SplashScreen 类添加 继承 Activity  如下

class SplashScreen : Activity
    {
    }

8、将以下 触发代码添加进去  然后保存

   class SplashScreen : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            var intent = new Intent(this, typeof(MainActivity));
            StartActivity(intent);
            Finish();
        }
    }

9、在项目下 找到 MainActivity.cs 类文件  打开 将 MainLauncher = true 删除,注意多余的 逗号也要删除

10、生成APP,  生成>重新生成 SplashScreen.Android>  生成成功  显示如下 表示 初步代码没有错误,如果生成失败请仔细检查是否漏了啥

11、生成安卓的APK安装包(如果是在模拟器调试 可以跳过第10步  直接点运行 测试效果)

    1)双击项目下的 Properties

2) 选择 Android清单 选择 图标 和你的最低版本 和 安装位置

3) 选择 Android选项  配置 选 Release  ,把 启用开发者检测  去勾

4)生成>重新生成 SplashScreen.Android  (重新生成你的项目) ,生成成功的结果和 第10步一样

5)生成APK 

      1)将Debug模式改为Release模式

       2)生成>存档> 等待一段时间> 生成成功如下>

             3)点击 发布>临时> 点击  +号 创建证书 按着要求 输入  完全自定义 不用照抄我的 ,密码自己记得就行了

                 

点击创建后,自动返回,然后选中创建好的证书 >另存为 >选要保存的位置,然后输入刚才证书的密码 点确定 就生成APK了 ,然后拷到手机去安装看效果吧,如果途中出现问题 无法解决的话  可以下载我的源码 研究研究   源码见最顶部

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐