我是初学者,反应原生,所以可能我的问题似乎对所有专家都很愚蠢 .
但我正在努力实现我想要实现的基本功能,我想用启动画面启动我的应用程序,几秒钟后我想显示登录屏幕或主屏幕 .
我检查了一些示例,但没有找到任何完整代码的示例,所以不知道如何在我的应用程序中使用这些代码片段 .
我已经尝试按照文档应用一些代码,但我的代码给出错误,请看看并帮助我 .
以下是我的代码:
Index.android.js
/**
* Sample React Native App
* https://github.com/facebook/react-native
* @flow
*/
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
Navigator
} from 'react-native';
import Splash from './Splash';
import Login from './Login';
export default class DigitalReceipt extends Component {
render() {
return (
{
if (route.sceneConfig) {
return route.sceneConfig;
}
return Navigator.SceneConfigs.FloatFromRight;
}} />
);
}
renderScene(route, navigator) {
var routeId = route.id;
if (routeId === 'Splash') {
return (
);
}
if (routeId === 'Login') {
return (
);
}
return this.noRoute(navigator);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
});
AppRegistry.registerComponent('DigitalReceipt', () => DigitalReceipt);
Splash.js
import React, { Component } from 'react';
import {
AppRegistry,
View,
Text,
StyleSheet,
Image
} from 'react-native';
import { StackNavigator } from 'react-navigation';
import Login from './Login';
class Splash extends Component{
componentWillMount() {
var navigator = this.props.navigator;
setTimeout(() => {
navigate('Login')
}, 1000);
}
render(){
const { navigate } = this.props.navigation;
return (
Digital Receipt
Powered by React Native
);
}
}
const SplashApp = StackNavigator({
Login: { screen: Login },
Splash: { screen: Splash },
});
const styles = StyleSheet.create({
wrapper: {
backgroundColor: '#FFFFFF',
flex: 1,
justifyContent: 'center',
alignItems: 'center'
},
title: {
color: '#2ea9d3',
fontSize: 32,
fontWeight: 'bold'
},
subtitle:{
color: '#2ea9d3',
fontWeight: '200',
paddingBottom: 20
},
titleWrapper:{
flex: 1,
justifyContent: 'center',
alignItems: 'center'
},
logo:{
width: 96,
height: 96
}
});
AppRegistry.registerComponent('SplashApp', () => SplashApp);
Login.js
import React, { Component } from 'react';
import {
AppRegistry,
View,
Text,
StyleSheet,
Image
} from 'react-native';
import { StackNavigator } from 'react-navigation';
import Splash from './Splash';
class Login extends Component{
static navigationOptions = {
title: 'Welcome',
};
render(){
const { navigate } = this.props.navigation;
return (
Login Screen
);
}
}
const LoginApp = StackNavigator({
Login: { screen: Login },
Splash: { screen: Splash },
});
const styles = StyleSheet.create({
wrapper: {
backgroundColor: '#FFFFFF',
flex: 1,
justifyContent: 'center',
alignItems: 'center'
},
title: {
color: '#2ea9d3',
fontSize: 32,
fontWeight: 'bold'
}
});
AppRegistry.registerComponent('LoginApp', () => LoginApp);
如果您发现任何问题,请帮助我,对不起代码中的愚蠢错误 .
谢谢
4 回答
你总是可以用原生的方式做到这一点:
Fisrt您需要在不同设备上显示闪屏的图像:
LDPI:
肖像:200x320px
风景:320x200px
MDPI:
肖像:320x480px
风景:480x320px
HDPI:
肖像:480x800px
风景:800x480px
XHDPI:
肖像:720px1280px
风景:1280x720px
XXHDPI:
肖像:960px1600px
风景:1600x960px
XXXHDPI:
肖像:1280px1920px
风景:1920x1280px
它们必须是
png
格式,然后将它们放在android/app/src/main/res/drawable
上并创建一个以每个图像的分辨率命名的文件夹 . 例如:drawable/drawable-hdpi
.然后在drawable文件夹中,你必须创建一个名为
background_splash.xml
的文件,并输入如下内容:之后你必须在
android/app/res/values/styles.xml
中添加一个新样式更新
AndroidManifest.xml
文件,添加名为SplashActivity
的新活动并添加android:theme="@style/SplashTheme"
. 现在创建一个名为MainActibity
的空活动 . 你的AndroidManifest.xml
应该是这样的:现在我们需要告诉SplashActivity转到MainActivity,它代表我们的实际应用程序 . 为此,您需要创建一个名为
SplashActivity
的新Java类 .现在你应该看到一个闪屏 .
https://medium.com/handlebar-labs/how-to-add-a-splash-screen-to-a-react-native-app-ios-and-android-30a3cec835ae
只需遵循这个简单的代码 .
App.js
Splash.js
希望这可以帮助 :)
您可以尝试这个例子 . 在启动画面中不需要stacknavigator .
制作Splash Screen的正确方法是修改根路由 . Firstable你需要为你的应用程序创建图像 . 你可以通过在网站https://apetools.webprofusion.com/app/#/上传你的图像来快速地做到这一点,它将创建一个包含iOs,windows和android的所有图像的包文件,并将它们放在每个设备的文件夹中 . 您需要将这些文件夹复制到每个设备的路径,以防您创建任何文件夹,替换它 . 路线
{device}/app/src/main/res/{folder-name}
在路径
{device}/app/src/main/res/drawable
的drawable文件夹中,您将icon.png
&screen.png
并创建一个名为的文件splash_background.xml
在此文件中添加下一个文本在路线
{device}/app/src/main/java/com/{name-of-project}
中添加名为SplashActivity.java
的文件在此文件中
SplashActivity.java
添加下一个:在路线
{device}/app/src/main/res/values/styles.xml
中更改如下:并在路线
{device}/app/src/main/res/AndroidManifest.xml
更改如下现在通过在命令行中运行来重建您的应用程序
react-native run-android