javascript - 在 react native expo 中使用 flex 将同一行上的 2 张卡片与动态内容对齐
问题描述
我在 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>
)}}
解决方案
尝试给 FlatListnumColumns={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>
);
}
}
工作示例:世博小吃
更多信息,numcolumns
推荐阅读
- javascript - 无法从作为类型模块导入的 js 文件中调用函数
- typescript - 允许 TypeScript 从工厂推断类型
- javascript - 谷歌地图未显示在嵌套 div 中
- c# - Math.Round in linq to entity vs linq to objects
- azure - Azure 部署槽 - 向槽添加权限
- r - 在 R 中,生成具有应用于多个变量的概率的加权样本
- python - 具有破折号的数字模式的 Python 正则表达式
- excel - VBA 在特定工作表中搜索错误并将结果传回主宏
- javascript - 如何使用 jquery 从 json 数组中提取数据并将其附加到 html 表中?
- java - 我的自行车 Java 程序设置有问题