【问题标题】:Align 2 cards on the same row with dynamic content using flex in react native expo在 react native expo 中使用 flex 将同一行上的 2 张卡片与动态内容对齐
【发布时间】:2021-03-23 04:39:23
【问题描述】:

我在 react native expo 项目中使用卡片,卡片是动态创建和放置的,但我希望 2 张卡片显示在同一行。


所需代码


let data = [
{ id: "1", title: "First", desc: "Some desc", time: "4pm" },
{ id: "2", title: "Second", desc: "Some second desc", time: "5pm" },
           ];

class App extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            products: data,
        };
    }    

    render() {
        return (
            <View style={{display: "flex", flex: 1}}>
                    
                <View style={{}}>
                    <FlatList
                        data={this.state.products}
                        renderItem={({ item }) => (
                            <TouchableOpacity>
                                <Card
                                    style={{
                                        margin: 5,
                                    }}
                                >
                                    <View style={styles.checkCards}>
                                        <Text>{item.title}</Text>
                                    </View>
                                </Card>
                            </TouchableOpacity>
                        )}
                    />
                </View>
            </View>
           )}}

【问题讨论】:

    标签: javascript react-native flexbox expo


    【解决方案1】:

    尝试给 FlatList numColumns={2}

    let data = [
      { id: '1', title: 'First', desc: 'Some desc', time: '4pm' },
      { id: '2', title: 'Second', desc: 'Some second desc', time: '5pm' },
    ];
    
    export default class App extends React.Component {
      constructor(props) {
        super(props);
        this.state = {
          products: data,
        };
      }
    
      render() {
        return (
          <View style={{ display: 'flex', flex: 1, marginTop: 24 }}>
            <View style={{}}>
              <FlatList
              numColumns={2}
                data={this.state.products}
                renderItem={({ item }) => (
                  <TouchableOpacity style={{ flex: 1, }}>
                    <Card
                      style={{
                        margin: 5,
                        padding: 10,
                      }}>
                      <View style={{}}>
                        <Text>{item.title}</Text>
                      </View>
                    </Card>
                  </TouchableOpacity>
                )}
              />
            </View>
          </View>
        );
      }
    }
    
    

    工作示例:Expo Snack

    更多信息,numcolumns

    【讨论】:

      猜你喜欢
      • 2018-01-31
      • 2021-02-28
      • 1970-01-01
      • 2021-02-28
      • 2021-12-01
      • 2021-06-17
      • 1970-01-01
      • 2022-09-29
      • 2021-03-15
      相关资源
      最近更新 更多