1、安装插件
1、npm i react-native-splash-screen --save
2、react-native link react-native-splash-screen
2、执行完上面两条命令,在android/settings.gradle文件下会自动下面语句,请将下面的路径
中’…\node_modules\rn-splash-screen\android’单斜线改为双斜线表示
include ':rn-splash-screen'
project(':rn-splash-screen').projectDir = new File(rootProject.projectDir, '..\\node_modules\\rn-splash-screen\\android')
3、:在android中进行相关配置
1、在android/app/src/main/java/com/xxx/MainActivity.java 文件中添加所示的代码
package xxxx;
import android.os.Bundle; // 添加 第一处
import com.facebook.react.ReactActivity;
import org.devio.rn.splashscreen.SplashScreen; // 添加 第二处
public class MainActivity extends ReactActivity {
/**
* Returns the name of the main component registered from JavaScript.
* This is used to schedule rendering of the component.
*/
@Override
protected void onCreate(Bundle savedInstanceState) { // 添加 第三处
SplashScreen.show(this);
super.onCreate(savedInstanceState);
} //第三处
@Override
protected String getMainComponentName() {
return "xxx";
}
}
4、android/app/src/main/res目录中,新建layout目录,在layout目录下,创建launch_screen.xml文件,
复制以下内容( 其中 launch_screen 为启动的图片名 )
<?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>
5、在android/app/src/main/res目录中,新建drawable-xhdpi和drawable-xxhdpi目录,在其中放入启动页图片,
命名为launch_screen.png
6、打开React-native项目的主入口文件,也就是React-native项目/App.js文件中添加,表示启动页2S后关闭,进入app主页
import SplashScreen from 'react-native-splash-screen';
export default class App extends Component<Props> {
componentDidMount() { //只需添加componentDidMount(){}就行
setTimeout(() => {
SplashScreen.hide(); //关闭启动屏幕
}, 2000);
}
componentWillUnmount() {
this.timer && clearTimeout(this.timer); //清除计时器
}
render() {
return (
<View style={styles.container}>
<Text style={styles.welcome}>Welcome to React Native!</Text>
<Text style={styles.instructions}>To get started, edit App.js</Text>
<Text style={styles.instructions}>{instructions}</Text>
</View>
);
}
}