美文网首页
react-navigation 监听横竖屏从新布局

react-navigation 监听横竖屏从新布局

作者: 黎峰麟 | 来源:发表于2018-06-07 14:40 被阅读133次

1 监听布局改变

    组件内部拥有onLayout方法
    onLayout={this._onLayout}

    //如何动态布局呢
    //1 当然是监听横竖屏之后
    //2 使用this.setState 动态改变布局咯

    _onLayout(event){

        let {x,y,width,height} = event.nativeEvent.layout;
        console.log("X:"+x+"y:"+y+"width:"+width+"height:"+height);

        if(height>width){
            console.log("竖屏");
        }else{
            console.log("横屏");
        }
    }

2 改变状态机

<View style={styles.container}>
    <TouchableOpacity  onPress ={() => {this.setState({change:true})}}>
          {
            (this.state.change == true)
             ? (<Image source={'./images/img1.png'} /> )
             : (<Image  source={'./images/img2.png'} />)
           }
     </TouchableOpacity>
</View>

相关文章

网友评论

      本文标题:react-navigation 监听横竖屏从新布局

      本文链接:https://www.haomeiwen.com/subject/wedgsftx.html