flexDirection不适用于scrollview

时间:2018-04-27 09:42:06

标签: reactjs react-native flex-direction

我正在尝试使用ScrollView制作网格,但我没有得到预期的结果

<Text style={{width:100, height:100, backgroundColor:'red'}} >Text</Text>
        <Text style={{width:100, height:100, backgroundColor:'red'}} >Text</Text>
        <Text style={{width:100, height:100, backgroundColor:'red'}} >Text</Text>
        <Text style={{width:100, height:100, backgroundColor:'red'}} >Text</Text>
      </ScrollView>

结果: enter image description here

2 个答案:

答案 0 :(得分:1)

您应该为ScrollView contentContainerStyle添加样式以使其具有flexDirection:'row'和flexWrap:'wrap'

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

export default class App extends Component {
  render() {
    return (
      <ScrollView contentContainerStyle={styles.container}>
        <Text style={styles.cell}>
        </Text>
      </ScrollView>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flexDirection: 'row',
    flexWrap: 'wrap'
  },
  cell: {
    width: 100,
    height: 100,
    backgroundColor: 'red'
  },
});

答案 1 :(得分:0)

将每个文本换行到视图

<View>
   <Text style={{width:100, height:100, backgroundColor:'red'}}>Text</Text>
</View>