2016-09-20 87 views
0

我打算在我的Xamarin Android项目中创建一个闪屏。布局在全屏幕中不可见Xamarin Android中的活动

我有以下布局:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_gravity="center" 
    android:gravity="center" android:background="#11aaff"> 
    <ImageView 
     android:layout_gravity="center" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:src="@drawable/splash" /> 
</LinearLayout> 

下面的代码:

[Activity(Label = "My Xamarin App", MainLauncher = true, NoHistory = true, Theme = "@android:style/Theme.Light.NoTitleBar.Fullscreen", 
    ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)] 
    public class SplashScreenActivity : Activity 
    { 
    protected override void OnCreate(Bundle savedInstanceState) 
    { 
     base.OnCreate(savedInstanceState); 
     SetContentView(Resource.Layout.SplashScreen); 
     // Create your application here 
     //var intent = new Intent(this, typeof(MainActivity)); 
     //StartActivity(intent); 
     //Finish(); 
    } 

    protected override void OnStart() 
    { 
     base.OnStart(); 
     // Create your application here 
     var intent = new Intent(this, typeof(MainActivity)); 
     StartActivity(intent); 
    } 
    } 

启动应用程序后,我得到一个白屏(注意主题)和我的第二个活动( MainActivity)几秒钟后显示。

如果我删除StartActivity并仅显示启动画面,它将显示白色屏幕约1-2秒,然后显示图像和蓝色空白(如预期) - 显然第二个活动未启动。

我应该怎么做才能让布局立即出现?

回答

1

您可以使用自定义主题,而不是自定义布局。

只是注意,此解决方案工作,你必须添加以下金块包到项目: Xamarin.Android.Support.v4Xamarin.Android.Support.v7.AppCompat 项记载参考链接在下面。

我必须这样做而回,并使用该链接作为参考: Creating a 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"/><!-- Your BG color here --> 
    </item> 
    <item> 
    <bitmap 
     android:src="@drawable/splash"<!-- your splash screen image here --> 
     android:tileMode="disabled" 
     android:gravity="center"/> 
    </item> 
</layer-list> 

然后编辑styles.xml文件(默认为Resources/values),并添加:

<style name="MyTheme.Splash" parent ="Theme.AppCompat.Light"> 
    <item name="android:windowBackground">@drawable/splash_screen</item><!-- here you should put the name of the file you just created in the drawable folder --> 
    <item name="android:windowNoTitle">true</item> 
    <item name="android:windowFullscreen">true</item> 
</style> 

最后您的初始屏幕应扩大AppCompatActivity而不是活动和主题应该是你的自定义,像这样:

[Activity(Label = "My Xamarin App", MainLauncher = true, NoHistory = true, Theme = "@style/MyTheme.Splash", 
    ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)] 
    public class SplashScreenActivity : AppCompatActivity 

我希望这有助于。

+0

谢谢,我真的应该使用官方的Xamarin方法在第一位:) – Nestor