(友情提示:RN学习,从最基础的开始,大家不要嫌弃太基础,会的同学请自行略过,希望不要耽误已经会的同学的宝贵时间)
Modal是模态视图,它的作用是可以用来覆盖 React Native中根视图的原生视图,Modal模态视图是一种覆盖包围当前内容视图的一个简单方法。
注意:如果你需要如何在您的应用程序的其余部分呈现模态的更多控制,那么可以考虑使用顶级导航(top-level Navigator)。
照例,我想大家都知道我的习惯了,毕竟官网也是这个顺序,那就是在用人之前,先要了解人,毕竟疑人不用,用人不疑嘛,要想相信一个人,首先得了解一个人嘛。来,看看 Modal 的相关属性。
来,我们大家一起看看这个效果的实现,看完效果就更加直观的能够感受到这个组件的作用和功能了。动态效果图如下:
import React, { Component } from 'react'; import { AppRegistry, StyleSheet, Modal, Picker, Switch, TouchableHighlight, Text, View } from 'react-native'; class Button extends Component { state = { active: false, }; _onHighlight = () => { this.setState({active: true}); }; _onUnhighlight = () => { this.setState({active: false}); }; render() { var colorStyle = { color: this.state.active ? '#fff' : '#000', }; return ( <TouchableHighlight onHideUnderlay={this._onUnhighlight} onPress={this.props.onPress} onShowUnderlay={this._onHighlight} style={[styles.button, this.props.style]} underlayColor="#a9d9d4"> <Text style={[styles.buttonText, colorStyle]}>{this.props.children}</Text> </TouchableHighlight> ); } } export default class ModalDemo extends Component { state = { animationType: 'none', modalVisible: false, transparent: false, }; _setModalVisible = (visible) => { this.setState({modalVisible: visible}); }; _setAnimationType = (type) => { this.setState({animationType: type}); }; _toggleTransparent = () => { this.setState({transparent: !this.state.transparent}); }; render() { var modalBackgroundStyle = { backgroundColor: this.state.transparent ? 'rgba(0, 0, 0, 0.5)' : '#f5fcff', }; var innerContainerTransparentStyle = this.state.transparent ? {backgroundColor: '#fff', padding: 20} : null; var activeButtonStyle = { backgroundColor: '#ddd' }; return ( <View> <Modal animationType={this.state.animationType} transparent={this.state.transparent} visible={this.state.modalVisible} onRequestClose={() => this._setModalVisible(false)} > <View style={[styles.container, modalBackgroundStyle]}> <View style={[styles.innerContainer, innerContainerTransparentStyle]}> <Text>This modal was presented {this.state.animationType === 'none' ? 'without' : 'with'} animation.</Text> <Button onPress={this._setModalVisible.bind(this, false)} style={styles.modalButton}> Close </Button> </View> </View> </Modal> <View style={styles.row}> <Text style={styles.rowTitle}>Animation Type</Text> <Button onPress={this._setAnimationType.bind(this, 'none')} style={this.state.animationType === 'none' ? activeButtonStyle : {}}> none </Button> <Button onPress={this._setAnimationType.bind(this, 'slide')} style={this.state.animationType === 'slide' ? activeButtonStyle : {}}> slide </Button> <Button onPress={this._setAnimationType.bind(this, 'fade')} style={this.state.animationType === 'fade' ? activeButtonStyle : {}}> fade </Button> </View> <View style={{marginTop:50,flexDirection: 'row',alignItems: 'center',justifyContent: 'center'}}> <Text style={{color:'grey',fontWeight: 'bold',marginRight:20}}>Transparent</Text> <Switch value={this.state.transparent} onValueChange={this._toggleTransparent} /> </View> <Button onPress={this._setModalVisible.bind(this, true)}> Present </Button> </View> ); } } const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', padding: 20, }, innerContainer: { borderRadius: 10, alignItems: 'center', }, row: { alignItems: 'center', flex: 1, flexDirection: 'row', marginBottom: 20, }, rowTitle: { flex: 1, fontWeight: 'bold', }, button: { borderRadius: 5, flex: 1, height: 44, alignSelf: 'stretch', justifyContent: 'center', overflow: 'hidden', }, buttonText: { fontSize: 18, margin: 5, textAlign: 'center', }, modalButton: { marginTop: 10, }, pickerItem: { fontSize: 16, }, }); AppRegistry.registerComponent('ModalDemo', () => ModalDemo);
这个例子内容比较多,大家仔细看看,最好动手实践一下,这样才能掌握的更加熟练。
本文分享自微信公众号 - 非著名程序员(non-famous-coder),作者:loonggg
原文出处及转载信息见文内详细说明,如有侵权,请联系 yunjia_community@tencent.com 删除。
原始发表时间:2017-01-05
本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。
我来说两句