react native Android启动页面、修改图标、修改名字、修复启动白屏

转自:https://blog.csdn.net/sinat_17775997/article/details/70347606

给Android添加启动页

实现启动页基本有三种思路:

  • 使用RN开源组件;
  • 原生java编写;
  • 模拟启动页,这种方法基本就是replace路由栈;http://www.jianshu.com/p/da658aceeb44

我们之所以设置启动页,很大一部分原因是在启动页显示的背后可以利用宝贵的时间来初始化我们的应用,启动页消失后,初始化的工作就应该做完。因此,使用开源RN组件是比较靠谱的,闲言少叙,直奔主题!

我们使用rn-splash-screen组件,其他组件比如react-native-splash-screenreact-native-splashscreen等配置过程都会出现很多坑,只有这个坑最少。

当然了,react-native-splash-screen还是星星最多的,只不过初次配置太多问题了,以后有时间再回来收拾他。

使用步骤:

  1. 安装 npm install --save rn-splash-screen
  2. 连接 react-native link rn-splash-screen
  3. 在res文件中新建drawable文件夹,放置splash.png;
  4. 修改android/app/src/main/res/values/styles.xml文件,添加一行:
     
    1. <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

    2. + <item name="android:windowBackground">@drawable/splash</item>

    3. </style>

    5.修改android/app/src/main/AndroidManifest.xml文件:
 
  1. android:name=".MainApplication"

  2. android:allowBackup="true"

  3. android:label="@string/app_name"

  4. - android:icon="@mipmap/ic_launcher"

  5. - android:theme="@style/AppTheme">

  6. + android:icon="@mipmap/ic_launcher">

  7. <activity

  8. android:name=".MainActivity"

  9. android:label="@string/app_name"

  10. + android:theme="@style/AppTheme"

  11. android:configChanges="keyboard|keyboardHidden|orientation|screenSize">

  12. <intent-filter>

  13. <action android:name="android.intent.action.MAIN" />

6.修改android/app/src/main/java/com/APPNAMES/MainActivity.java文件:

 
  1. import android.graphics.Color;

  2. import android.os.Bundle;

  3.  
  4. import com.facebook.react.ReactInstanceManager;

  5. import com.facebook.react.bridge.ReactContext;

  6. import com.mehcode.reactnative.splashscreen.SplashScreen;

  7.  
  8. public class MainActivity extends ReactActivity {

  9. // [...]

  10.  
  11. @Override

  12. protected void onCreate(Bundle savedInstanceState) {

  13. // Show the js-controlled splash screen

  14. SplashScreen.show(this, getReactInstanceManager());

  15.  
  16. super.onCreate(savedInstanceState);

  17.  
  18. // [...]

  19. }

  20.  
  21. // [...]

  22. }

7.在入口文件中测试:

rn-splash-screen提供了两个API,open()和hide()。

 
  1. /**

  2. * Sample React Native App

  3. * https://github.com/facebook/react-native

  4. * @flow

  5. */

  6.  
  7. import React, { Component } from 'react';

  8. import {

  9. AppRegistry,

  10. StyleSheet,

  11. Text,

  12. View

  13. } from 'react-native';

  14. // 引入引导页组件

  15. import SplashScreen from 'rn-splash-screen';

  16.  
  17. export default class splashTest extends Component {

  18.  
  19. constructor(props){

  20. super(props);

  21. this.state = {};

  22. }

  23.  
  24. componentDidMount() {

  25. setTimeout(() => {

  26. SplashScreen.hide();

  27. }, 2000);

  28. }

  29.  
  30. render() {

  31. return (

  32. <View style={styles.container}>

  33. <Text style={styles.welcome}>

  34. Welcome to React Native!

  35. </Text>

  36. </View>

  37. );

  38. }

  39. }

  40.  
  41. const styles = StyleSheet.create({

  42. container: {

  43. flex: 1,

  44. justifyContent: 'center',

  45. alignItems: 'center',

  46. backgroundColor: '#F5FCFF',

  47. },

  48. welcome: {

  49. fontSize: 20,

  50. textAlign: 'center',

  51. margin: 10,

  52. },

  53. instructions: {

  54. textAlign: 'center',

  55. color: '#333333',

  56. marginBottom: 5,

  57. },

  58. });

  59.  
  60. AppRegistry.registerComponent('splashTest', () => splashTest);

注意:react-native run-android过程中很一直报错,如果按照以上方法修改的话,是没有问题的(RN 0.43),有效的办法是编译之前删除android\app\build文件夹下的四个文件夹,这样就不会重复报错了。

修改APP的名称

很简单,我们直接打开android/app/src/main/res/values/strings.xml,即可看到配置中的app_name,修改为你想要的即可:

 
  1. <resources>

  2. <string name="app_name">随遇而安</string>

  3. </resources>

修改App的图标

也很简单,在android\app\src\main\res\mipmap-xxxxxx中直接覆盖图标就可以,注意图标的大小。

最终效果图:

app.gif

猜你喜欢

转载自blog.csdn.net/m0_38129431/article/details/88191161