2017-10-07 121 views
2

我一直在尋找正確的方式來避免白屏 - 用閃屏替換 - 當我的Android應用程序啓動時。 我發現,使用樣式,比如在啓動時擺脫白屏

<style name="AppTheme.SplashTheme"> 
    <item name="android:windowNoTitle">true</item> 
    <item name="android:windowActionBar">false</item> 
    <item name="android:windowFullscreen">true</item> 
    <item name="android:windowContentOverlay">@null</item> 
    <item name="android:windowIsTranslucent">true</item> 
</style> 

解決方案,但在我的情況下,這只是延遲從外觀上看,作爲應用程序無法正常工作(視圖幾秒鐘後膨脹)。我不想要一些持續幾秒鐘的視圖,我已經決定,我只是想在我的應用程序需要加載時將空白屏幕替換爲自定義內容。 獲得所需行爲的唯一方法是在我的SplashscreenActivity樣式中放置一個可繪製的背景。

<item name="android:windowBackground">@drawable/background_splash</item> 

現在,我不想放置圖像作爲背景 - 因爲屏幕的不同分辨率 - 但要設置xml佈局。 這可能嗎?在不延遲視圖外觀的情況下襬脫黑屏的最佳方式是什麼?

回答

2

如下

  • res/drawable文件夾中創建一個XML文件下面的內容你應該這樣做。這將作爲你的啓動畫面。讓我們將其命名爲splash.xml

    <?xml version="1.0" encoding="utf-8"?> 
    
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
        <item android:drawable="@color/colorPrimary" /> 
        <item> 
         <bitmap android:src="@drawable/splash_logo" 
        android:gravity="center" /> 
        </item> 
    </layer-list> 
    
  • 然後,你需要指定一個style/theme沒有標題欄爲您飛濺視圖,所以下面style添加到您的style.xml文件。

    <style name="SplashTheme" parent="Theme.AppCompat.NoActionBar"> 
        <item name="android:windowBackground">@drawable/splash</item> 
    </style> 
    
  • AndroidManifest.xml改變你的launcher activitytheme使用SplashTheme

    <activity 
        android:name=".MainActivity" 
        android:label="@string/app_name" 
        android:theme="@style/SplashTheme"> 
        <intent-filter> 
         <action android:name="android.intent.action.MAIN" /> 
    
         <category android:name="android.intent.category.LAUNCHER" /> 
        </intent-filter> 
    </activity> 
    
  • 最後在launcher activity(在我的例子MainActivity)更改主題返回到默認的應用程序的主題,通常命名爲AppTheme

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        setTheme(R.style.AppTheme) 
        super.onCreate(savedInstanceState);  
        ... 
    } 
    

就是這樣!現在運行你的應用程序,看看沒有白屏了。

參考文獻:我用this postthis one來提供上面的代碼示例。

+0

在飛濺xml我不能將LinearLayouts,Fabs或其他組件正確?因爲我想讓我的splashscreen看起來像MainActivity –

+0

不,我想你不能。如果您使用具有此類項目的佈局,則應用程序首次啓動時需要時間,因此白色屏幕再次出現。 – Merka

+0

所以,如果我需要一個屏幕分裂成4種不同的顏色? –