先講下我的RN版本0.58.5
首先安裝react-native-splash-screen(目前使用的版本是3.2.0)
項目地址https://github.com/crazycodeboy/react-native-splash-screen
原理參考作者的文章:https://www.jianshu.com/p/78571e5435ec
安裝了這個組件后,可以解決掉RN的啟動白屏,但是啟動時仍然會有一小段的白屏,
這個是ANDROID本身的白屏,要解決掉這個白屏
需要修改android目錄下app/src/main/res/values/styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <item name="android:windowTranslucentStatus">true</item> + <item name="android:statusBarColor">@android:color/transparent</item + <item name="android:windowBackground">@drawable/launch_screen</item> + <item name="android:windowFullscreen">true</item> </style>
其中有 + 號的行為后增加的行,其主要原理就是ANDROID啟動時先設置一個背景,這里面我們把背景設成和react-native-splash-screen組件一樣的背景,
<item name="android:windowBackground">@drawable/launch_screen</item>
這樣設置完成后,啟動時確實沒有白屏了,但是有一個問題,我們在啟動時的背景是全屏,沒有標題欄,但是當react-native-splash-screen的背景啟動時,就會出現標題欄,這時候圖片就會有一個向下的位移,要解決這個問題,需要改下react-native-splash-screen的源碼。
找到node_modules/react-native-splash-screen/android/src/main/res/values/styles.xml
<resources> <style name="SplashScreen_SplashAnimation"> <item name="android:windowExitAnimation">@android:anim/fade_out</item> </style> <style name="SplashScreen_SplashTheme" parent="Theme.AppCompat.NoActionBar"> <item name="android:windowAnimationStyle">@style/SplashScreen_SplashAnimation</item> + <item name="android:windowNoTitle">true</item> + <item name="android:windowTranslucentStatus">true</item> </style> <style name="SplashScreen_Fullscreen" parent="SplashScreen_SplashTheme"> <item name="android:windowFullscreen">true</item> </style> </resources>
同樣的帶 + 號的行是增加的行