ReactNative→FlexBox布局

Flexbox在布局中能够解决什么问题?

  • 浮动布局
  • 各种机型屏幕的适配
  • 水平和垂直居中
  • 自动分配宽度

一、简单的运用Flexbox


当没有超出屏幕宽度时居中显示
当超出屏幕宽度时居中显示
export default class helloworld extends Component {

  render() {

    return (
      <View  style={styles.container}>

        {/*{this.renderBags()}*/}

        <View style={{width:80, height: 50, backgroundColor:'blue', alignSelf:'flex-end'}}></View>
        <View style={{width:50, height: 100, backgroundColor:'green'}}></View>
        <View style={{width:50, height: 50, backgroundColor:'yellow'}}></View>
        <View style={{width:90, height: 350, backgroundColor:'black'}}></View>
        <View style={{width:50, height: 80, backgroundColor:'yellow'}}></View>
        {/*<View style={{width:60, height: 100, backgroundColor:'green'}}></View>*/}
        {/*<View style={{width:80, height: 50, backgroundColor:'red', }}></View>*/}
        {/*<View style={{width:90, height: 180, backgroundColor:'black', alignSelf:'flex-end'}}></View>*/}
      </View>
    );
  }

const styles = StyleSheet.create({
  container: {
    flex: 1,
    // 主轴对齐方式
    justifyContent: 'center',
    // 侧轴对齐方式
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
    // 改变主轴方向
    flexDirection: 'row',
    // 换行显示
    flexWrap:'wrap',
  },
})

这里有个疑问:超过屏幕宽度时没有达到我想要的效果

二、获取 屏幕宽度参数

var Dimensions = require('Dimensions');
var {width, height, scale} = Dimensions.get('window');
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 13,806评论 1 92
  • what's the meaning of Flexbox? 答:很久之前关于布局大概存在两种:流式布局,定位和浮...
    不误正业的开发者阅读 400评论 0 2
  • 关于FlexBox布局,我的理解上,先确定以谁为主,谁未次,然后根据主次方向开始布局。 ReactNative中文...
    浪高达阅读 282评论 0 0
  • 1、flexDirection 决定布局的主轴(水平轴x) 默认值是竖直轴(column) 决定子元素是应该沿着水...
    Loki_阅读 218评论 0 0
  • 使用flexDirection、alignItems和 justifyContent 三个样式属性就已经能满足大多...
    linzaifei阅读 248评论 0 0