2016-09-20 91 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