【发布时间】:2020-06-08 18:25:14
【问题描述】:
- 使用
flexbox(因为它是react-native) - 父容器的宽度是一个百分比
- 项目数可以是 4/5/6/7..
- 项目之间的间距相同
- 如下图所示
这是我的代码:
const styles = {
container: {
width: screenWidth * 0.9,
flexWrap: 'wrap',
flexDirection: 'row'
},
item: {
backgroundColor: 'red',
height: 120,
width: (width * 0.9 - 20) / 3,
marginBottom: 10
}
}
<View style={styles.container}>
{items.map((item, idx) =>
<View style={[styles.item, { marginHorizontal: idx % 3 === 1 ? 10 : 0}]} />
)}
</View>
还有其他更好的方法来实现这种布局吗?
【问题讨论】:
-
你试过
justifyContent: 'space-between'吗? -
@AkhilAravind 不,你可以考虑有 5 项。
-
@MayankPandav 这也是一个选项。但首先我需要将数组分成三份。
-
您已经根据条件拆分了 3 个数组
标签: css reactjs react-native flexbox