Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
React native 如何在react native上创建两列网格?_React Native_Grid_Styles - Fatal编程技术网

React native 如何在react native上创建两列网格?

React native 如何在react native上创建两列网格?,react-native,grid,styles,React Native,Grid,Styles,我现在的任务是制作一个屏幕,在两列的网格中提供选项,并将多张卡片作为列表的项目。(你可以看到) 我试图创建一个行flexbox,但它最终只是一直水平地继续 我想知道,当两列向下展开时,有什么好方法可以达到这种效果。您只需执行以下操作即可: <View style={{flex:1, flexDirection:"row", maxHeight:100}}> <View style={{flex : 1, backgroundColor:

我现在的任务是制作一个屏幕,在两列的网格中提供选项,并将多张卡片作为列表的项目。(你可以看到)

我试图创建一个行flexbox,但它最终只是一直水平地继续


我想知道,当两列向下展开时,有什么好方法可以达到这种效果。

您只需执行以下操作即可:

        <View style={{flex:1, flexDirection:"row", maxHeight:100}}> 
            <View style={{flex : 1, backgroundColor:"red"}}/>
            <View style={{flex : 1, backgroundColor:"yellow"}}/>
       </View>


然后,添加新视图将使您实现这样的布局,然后由您决定要渲染的内容和数量

您只需执行以下操作即可实现此目的:

        <View style={{flex:1, flexDirection:"row", maxHeight:100}}> 
            <View style={{flex : 1, backgroundColor:"red"}}/>
            <View style={{flex : 1, backgroundColor:"yellow"}}/>
       </View>

然后添加新视图将使您实现这样的布局,然后由您决定要渲染的内容和数量,并将prop设置为“2”以将平面列表显示为网格

这里是完整的代码示例

import React from "react";
import { SafeAreaView, FlatList, Text, View, Image } from "react-native";

const DATA = [
  {
    id: "1",
    title: "RUSTY DRIVE",
    image:
      "https://res.cloudinary.com/demo/image/upload/w_260,h_200,c_crop,g_north/sample.jpg"
  },
  {
    id: "2",
    title: "SABOR MORENO",
    image:
      "https://res.cloudinary.com/demo/image/upload/w_260,h_200,c_crop,g_north/sample.jpg"
  },
  {
    id: "3",
    title: "0 MESTRE PUB",
    image:
      "https://res.cloudinary.com/demo/image/upload/w_260,h_200,c_crop,g_north/sample.jpg"
  },
  {
    id: "4",
    title: "GRILL 54 CHEF",
    image:
      "https://res.cloudinary.com/demo/image/upload/w_260,h_200,c_crop,g_north/sample.jpg"
  },
  {
    id: "5",
    title: "RUSTY DRIVE",
    image:
      "https://res.cloudinary.com/demo/image/upload/w_260,h_200,c_crop,g_north/sample.jpg"
  },
  {
    id: "6",
    title: "SABOR MORENO",
    image:
      "https://res.cloudinary.com/demo/image/upload/w_260,h_200,c_crop,g_north/sample.jpg"
  },
  {
    id: "7",
    title: "0 MESTRE PUB",
    image:
      "https://res.cloudinary.com/demo/image/upload/w_260,h_200,c_crop,g_north/sample.jpg"
  },
  {
    id: "8",
    title: "GRILL 54 CHEF",
    image:
      "https://res.cloudinary.com/demo/image/upload/w_260,h_200,c_crop,g_north/sample.jpg"
  }
];

export default class App extends React.Component {
  _renderItem = ({ item }) => (
    <View style={{ flex: 1, marginHorizontal: 20, marginBottom: 20 }}>
      <Image
        style={{ width: "100%", height: 140 }}
        source={{ uri: item.image }}
      />
      <Text style={{ textAlign: "center", marginTop: 8 }}>{item.title}</Text>
    </View>
  );

  render() {
    return (
      <SafeAreaView style={{ flex: 1 }}>
        <FlatList
          data={DATA}
          renderItem={this._renderItem}
          keyExtractor={item => item.id}
          numColumns={2}
          style={{ flex: 1 }}
          contentContainerStyle={{ paddingVertical: 20 }}
        />
      </SafeAreaView>
    );
  }
}

从“React”导入React;
从“react native”导入{SafeAreaView,FlatList,Text,View,Image};
常数数据=[
{
id:“1”,
标题:“生锈的驱动器”,
图片:
"https://res.cloudinary.com/demo/image/upload/w_260,h_200,c_crop,g_north/sample.jpg“
},
{
id:“2”,
标题:“萨博·莫雷诺”,
图片:
"https://res.cloudinary.com/demo/image/upload/w_260,h_200,c_crop,g_north/sample.jpg“
},
{
id:“3”,
标题:“0梅斯特酒吧”,
图片:
"https://res.cloudinary.com/demo/image/upload/w_260,h_200,c_crop,g_north/sample.jpg“
},
{
id:“4”,
标题:“烧烤54厨师”,
图片:
"https://res.cloudinary.com/demo/image/upload/w_260,h_200,c_crop,g_north/sample.jpg“
},
{
id:“5”,
标题:“生锈的驱动器”,
图片:
"https://res.cloudinary.com/demo/image/upload/w_260,h_200,c_crop,g_north/sample.jpg“
},
{
id:“6”,
标题:“萨博·莫雷诺”,
图片:
"https://res.cloudinary.com/demo/image/upload/w_260,h_200,c_crop,g_north/sample.jpg“
},
{
id:“7”,
标题:“0梅斯特酒吧”,
图片:
"https://res.cloudinary.com/demo/image/upload/w_260,h_200,c_crop,g_north/sample.jpg“
},
{
id:“8”,
标题:“烧烤54厨师”,
图片:
"https://res.cloudinary.com/demo/image/upload/w_260,h_200,c_crop,g_north/sample.jpg“
}
];
导出默认类App扩展React.Component{
_renderItem=({item})=>(
{item.title}
);
render(){
返回(
项目id}
numColumns={2}
style={{flex:1}}
contentContainerStyle={{paddingVertical:20}}
/>
);
}
}
应用程序预览

您应该使用并将prop设置为“2”以将平面列表显示为网格

这里是完整的代码示例

import React from "react";
import { SafeAreaView, FlatList, Text, View, Image } from "react-native";

const DATA = [
  {
    id: "1",
    title: "RUSTY DRIVE",
    image:
      "https://res.cloudinary.com/demo/image/upload/w_260,h_200,c_crop,g_north/sample.jpg"
  },
  {
    id: "2",
    title: "SABOR MORENO",
    image:
      "https://res.cloudinary.com/demo/image/upload/w_260,h_200,c_crop,g_north/sample.jpg"
  },
  {
    id: "3",
    title: "0 MESTRE PUB",
    image:
      "https://res.cloudinary.com/demo/image/upload/w_260,h_200,c_crop,g_north/sample.jpg"
  },
  {
    id: "4",
    title: "GRILL 54 CHEF",
    image:
      "https://res.cloudinary.com/demo/image/upload/w_260,h_200,c_crop,g_north/sample.jpg"
  },
  {
    id: "5",
    title: "RUSTY DRIVE",
    image:
      "https://res.cloudinary.com/demo/image/upload/w_260,h_200,c_crop,g_north/sample.jpg"
  },
  {
    id: "6",
    title: "SABOR MORENO",
    image:
      "https://res.cloudinary.com/demo/image/upload/w_260,h_200,c_crop,g_north/sample.jpg"
  },
  {
    id: "7",
    title: "0 MESTRE PUB",
    image:
      "https://res.cloudinary.com/demo/image/upload/w_260,h_200,c_crop,g_north/sample.jpg"
  },
  {
    id: "8",
    title: "GRILL 54 CHEF",
    image:
      "https://res.cloudinary.com/demo/image/upload/w_260,h_200,c_crop,g_north/sample.jpg"
  }
];

export default class App extends React.Component {
  _renderItem = ({ item }) => (
    <View style={{ flex: 1, marginHorizontal: 20, marginBottom: 20 }}>
      <Image
        style={{ width: "100%", height: 140 }}
        source={{ uri: item.image }}
      />
      <Text style={{ textAlign: "center", marginTop: 8 }}>{item.title}</Text>
    </View>
  );

  render() {
    return (
      <SafeAreaView style={{ flex: 1 }}>
        <FlatList
          data={DATA}
          renderItem={this._renderItem}
          keyExtractor={item => item.id}
          numColumns={2}
          style={{ flex: 1 }}
          contentContainerStyle={{ paddingVertical: 20 }}
        />
      </SafeAreaView>
    );
  }
}

从“React”导入React;
从“react native”导入{SafeAreaView,FlatList,Text,View,Image};
常数数据=[
{
id:“1”,
标题:“生锈的驱动器”,
图片:
"https://res.cloudinary.com/demo/image/upload/w_260,h_200,c_crop,g_north/sample.jpg“
},
{
id:“2”,
标题:“萨博·莫雷诺”,
图片:
"https://res.cloudinary.com/demo/image/upload/w_260,h_200,c_crop,g_north/sample.jpg“
},
{
id:“3”,
标题:“0梅斯特酒吧”,
图片:
"https://res.cloudinary.com/demo/image/upload/w_260,h_200,c_crop,g_north/sample.jpg“
},
{
id:“4”,
标题:“烧烤54厨师”,
图片:
"https://res.cloudinary.com/demo/image/upload/w_260,h_200,c_crop,g_north/sample.jpg“
},
{
id:“5”,
标题:“生锈的驱动器”,
图片:
"https://res.cloudinary.com/demo/image/upload/w_260,h_200,c_crop,g_north/sample.jpg“
},
{
id:“6”,
标题:“萨博·莫雷诺”,
图片:
"https://res.cloudinary.com/demo/image/upload/w_260,h_200,c_crop,g_north/sample.jpg“
},
{
id:“7”,
标题:“0梅斯特酒吧”,
图片:
"https://res.cloudinary.com/demo/image/upload/w_260,h_200,c_crop,g_north/sample.jpg“
},
{
id:“8”,
标题:“烧烤54厨师”,
图片:
"https://res.cloudinary.com/demo/image/upload/w_260,h_200,c_crop,g_north/sample.jpg“
}
];
导出默认类App扩展React.Component{
_renderItem=({item})=>(
{item.title}
);
render(){
返回(
项目id}
numColumns={2}
style={{flex:1}}
contentContainerStyle={{paddingVertical:20}}
/>
);
}
}
应用程序预览


您可以使用平面列表,将
numColumns
prop设置为2。您可以在
numColumns
prop设置为2的情况下使用平面列表。