admin_jim 2018-02-18
步骤一:npm i react-native-splash-screen@3.0.1 --save
react-native link react-native-splash-screen
步骤二:
Android:
更新你的 MainActivity.java 文件如下:
public class MainActivity extends ReactActivity { @Override
protected void onCreate(Bundle savedInstanceState) { SplashScreen.show(this); // 添加这一句
super.onCreate(savedInstanceState);
} // ...other code}iOS:
更新你的AppDelegate.m 文件如下:
#import "AppDelegate.h"
#import "RCTRootView.h"
#import "SplashScreen.h" // here
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{ // ...other code
[SplashScreen show]; // 添加这一句,这一句一定要在最后
return YES;
}@end
步骤三:
Android:
创建一个名为 launch_screen.xml 的布局文件放在android\app\src\main\res\layout来自定义你的启动屏幕。
然后把引导图 launch_screen.jpg 放在 android\app\src\main\res\drawable 文件夹下
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/launch_screen">
</LinearLayout>
然后启用app主题透明选项来解决在APP启动时因主题原因导致的短暂白屏的问题,具体步骤如下:
打开 android/app/src/main/res/values/styles.xml文件,添加 <item name="android:windowIsTranslucent">true</item>,如下 :
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<!--设置透明背景-->
<item name="android:windowIsTranslucent">true</item>
</style>
</resources>
如果你只做了以上步骤,你的程序会闪屏后退出,所以你需要在android\app\src\main\res\values\colors.xml文件中
写如下代码
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="primary_dark">#660B0B0B</color>
</resources>
做了以上步骤android启动屏就完成了
更详细的介绍,可以查看 examples https://github.com/crazycodeboy/react-native-splash-screen/tree/master/examples
iOS
iOS可以通过LaunchImage或LaunchScreen.xib来自定义你的启动屏幕。
更详细的介绍,可以查看 examples