简体   繁体   English

如何在React Native循环中对齐视图?

[英]How to align view in react native in loop?

Here is my React Native component code. 这是我的React Native组件代码。

import React, { Component } from 'react';
import {View, Text,StyleSheet} from 'react-native';

export default class AnatomyExample extends Component {
  render() {
    let data = [1,2,3,4,5,6,7,8];
    return (
      <View style={styles.main}>
        {data.map(this.renderView)}
      </View>


    );
  }

  renderView(d){
    return (
     <View style={styles.child}>
      <Text>{d}</Text>
       </View>
      )
  }
}



const styles = StyleSheet.create({
  main: {
    borderRadius: 4,
    borderWidth: 1,
    borderColor: '#d6d7da',
  },
  child: {
    borderWidth:1,
    borderColor:'blue',
    width:150,
    height:150,

  }
});

I want to make component output look like this image. 我想使组件输出看起来像这张图。 在此处输入图片说明

How to float View side by side. 如何并排浮动视图。 float:left; would work in html css but not sure how to make it in react native. 可以在html css中工作,但不确定如何使它在本机反应中运行。

You can add the styles like this, if you want 4 elements in a row else you can specify custom width 您可以添加这样的样式,如果要连续显示4个元素 ,则可以指定自定义宽度

import {Dimensions} from 'react-native';

const {width, height} = Dimensions.get('window')
main: {
    flexDirection: 'row',
    flexWrap: 'wrap',
    borderRadius: 4,
    borderWidth: 1,
    borderColor: '#d6d7da',
  },
  child: {
    margin: 5,
    borderWidth:1,
    borderColor:'blue',
    width: width / 4 - 12, // ... DeviceWidth / 4 - (marginLeft + marginRight + borderLeft + borderRight)
    height: 50,
    backgroundColor: 'gray'

  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM