css - FlatList 使用 2 列。我有奇数个项目要显示。如何让最后一个项目左对齐?
问题描述
所以我有一个 FlatList 组件,它呈现奇数个项目。FlatList 有 2 列,我正在使用'space-around'
列包装器。当项目数是偶数时,这很好用,但当它是奇数时,此列表中的最后一项将居中对齐。
因此,如果最后一行有一个项目,我如何让该项目左对齐(flex-start)?
<FlatList
columnWrapperStyle={ styles.columnWrapper }
data={ inStockItems }
keyExtractor={ item => item.itemId }
horizontal={ false }
numColumns={ 2 }
renderItem={ ({ item }) => (
<ItemContainer
// style={ styles.userStoreItem }
item={ item }
navigate={ this.props.navigation }
{ ...this.props }
admin
/>
) }
/>
styles.columnWrapper: {
justifyContent: 'space-around',
},
解决方案
您可以添加flex: 0.5
到项目容器:
<FlatList
columnWrapperStyle={{justifyContent:'space-between', }}
data={[{key: 'a'}, {key: 'b'}, {key: 'c'}]}
keyExtractor={item => item.itemId}
horizontal={false}
numColumns={2}
renderItem={({ item, index }) => (
<View style={{backgroundColor:'red', flex: 0.5, margin: 4}}>{/*Here*/}
<Text>{index}</Text>
</View>
)}
/>
推荐阅读
- node.js - 为什么我的服务器返回 JSON,但前端将它作为 HTML 接收?
- python - 为什么与键盘脚本的交互会使 Python 完全崩溃?
- python-3.x - 为什么我不能在 Pandas 直方图上获得标题?
- visual-studio - Visual Studio 2019 未检测到 .Net Core 3 pre-release 9 SDK
- browser - 我对新的 Twitch API 有一些疑问
- java - 自定义异常尝试在 Java 中抛出 Catch
- pointers - 如何在 JNA 中分配导出的全局指针数组
- swift - 为什么我的 for 循环索引向后运行?
- vba - 在Access VBA中,如何在删除之前验证父记录是否有子记录?
- pine-script - 在每个最后一个移动平均线交叉点(如 ZigZag)中获得最高点和最低点