1啟動圖
安裝第三方庫(react-native-splash-screen);
根據(jù)
react-native版本選擇不同的安裝包
React Native >= 0.47.0 use v3.+,
React Native < 0.47.0 use v2.1.0
yarn add react-native-splash-screen
or npm
npm i react-native-splash-screen --save
- 然后link
react-native link react-native-splash-screen
1手動配置找到目錄android/settings.gradle添加如下代碼
include ':react-native-splash-screen'
project(':react-native-splash-screen').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-splash-screen/android')
2找到目錄android/app/build.gradle添加如下代碼
...
dependencies {
...
implementation project(':react-native-splash-screen') //在對應位置添加這句
}
3 在android目錄下找到 MainApplication.java文件添加如下代碼添加時注意版本選擇
// react-native-splash-screen >= 0.3.1
import org.devio.rn.splashscreen.SplashScreenReactPackage;
// react-native-splash-screen < 0.3.1
import com.cboy.rn.splashscreen.SplashScreenReactPackage;
public class MainApplication extends Application implements ReactApplication {
private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
@Override
public boolean getUseDeveloperSupport() {
return BuildConfig.DEBUG;
}
@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
new SplashScreenReactPackage() //here
);
}
};
@Override
public ReactNativeHost getReactNativeHost() {
return mReactNativeHost;
}
}
4 在android目錄下找到 MainActivity.java文件添加如下代碼添加時注意版本選擇
import android.os.Bundle; // here
import com.facebook.react.ReactActivity;
// react-native-splash-screen >= 0.3.1
import org.devio.rn.splashscreen.SplashScreen; // here
// react-native-splash-screen < 0.3.1
import com.cboy.rn.splashscreen.SplashScreen; // here
public class MainActivity extends ReactActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
SplashScreen.show(this); // here
super.onCreate(savedInstanceState);
}
// ...other code
}
5在app/src/main/res/layout目錄下創(chuàng)建launch_screen.xml文件并添加如下代碼
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/launch_screen" android:scaleType="centerCrop" />
</RelativeLayout>
6在app/src/main/res/目錄下創(chuàng)建如下文件以適配在不同屏幕下選擇不同尺寸的圖片
- drawable-ldpi
- drawable-mdpi
- drawable-hdpi
- drawable-xhdpi
- drawable-xxhdpi
- drawable-xxxhdpi
修改app/src/main/res/values/colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="primary_dark">#000000</color>
</resources>
如果要設置啟動頁透明只需要修改android/app/src/main/res/values/styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<!--設置透明背景-->
<item name="android:windowIsTranslucent">true</item>
</style>
</resources>
配置完這些就算完成了,然后在對應的App初始頁關閉啟動圖就可以了
import SplashScreen from 'react-native-splash-screen'
export default class WelcomePage extends Component {
componentDidMount() {
// do stuff while splash screen is shown
// After having done stuff (such as async tasks) hide the splash screen
SplashScreen.hide(); // =>引入文件,調用該方法關閉啟動圖
}
}