React native (4)--props、state、style、布局
发布时间:2020-12-15 07:25:16 所属栏目:百科 来源:网络整理
导读:1.属性:props render() { return( Text Hello {this.props.name} / Text ); } 使用 View style = {{ flex :1, flexDirection :' column '}} Greeting name = "sinstar" / Greeting name = "cos" / / View 2 状态 state constructor(props) { super (props);
render() { return( <Text>Hello {this.props.name}</Text> ); } 使用 <View style={{flex:1,flexDirection:'column'}}> <Greeting name="sinstar"/> <Greeting name="cos"/> </View>
constructor(props) {
super(props);
this.state = {showText: true}
//每1000毫秒对showText状态做一次取反操作
setInterval(() => { this.setState({showText: !this.state.showText}); },1000); } render(){ let display = this.state.showText ? this.props.text : ' '; return( <Text>{display}</Text> ) } //使用 ----------------------------------- <BlinkTest text="aaa"/>
export default class Greeting extends Component { render() { return( <Text style={styles.red}>Hello {this.props.name}</Text> // <View style={{width:300,height:200,backgroundColor:'red'}}></View> // <View style={{flex: 1,backgroundColor: 'powderblue'}} /> // <View style={{flex: 2,backgroundColor: 'skyblue'}} /> // <View style={{flex: 3,backgroundColor: 'steelblue'}} /> ); } } const styles=StyleSheet.create({ red:{ color:'red',fontSize:30 } })
下面是布局
flexDirection:'column':竖向 flexDirection:'row' :横向 return ( <View style={{flex:1,flexDirection:'column'}}> <Greeting name="sinstar"/> <Greeting name="cos"/> <BlinkTest text="aaa"/> <View style={{width:300,backgroundColor:'red'}}></View> <View style={{flex: 1,backgroundColor: 'powderblue'}} /> <View style={{flex: 2,backgroundColor: 'skyblue'}} /> <View style={{flex: 3,backgroundColor: 'steelblue'}} /> </View> );
// 尝试把`justifyContent`改为`center`看看 // 尝试把`flexDirection`改为`row`看看 <View style={{ flex: 1,flexDirection: 'column',justifyContent: 'space-between',}}> <View style={{width: 50,height: 50,backgroundColor: 'powderblue'}} /> <View style={{width: 50,backgroundColor: 'skyblue'}} /> <View style={{width: 50,backgroundColor: 'steelblue'}} /> </View>
// 尝试把`alignItems`改为`flex-start`看看 // 尝试把`justifyContent`改为`flex-end`看看 // 尝试把`flexDirection`改为`row`看看 <View style={{ flex: 1,justifyContent: 'center',alignItems: 'center',backgroundColor: 'steelblue'}} /> </View> ); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |