doz*_*ozo 5 listview image padding react-native
我正在尝试在我正在处理的本机应用程序中向某些图像添加填充。根据设备宽度调整图像大小,以便每行显示四张。在此props.images中,显示了9张图像,没有任何填充。
我试图将图像包装在填充为1的视图中,如下所示:
renderRow(rowData){
const {uri} = rowData;
return(
<View style={{padding: 1}}>
<Image style={styles.imageSize} source={{uri: uri}} />
</View>
)
}
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试仅前三个图像出现填充时。其余图像不会出现。
我的整个代码在这里:
class ImageList extends Component {
componentWillMount(){
const ds = new ListView.DataSource({
rowHasChanged: (r1, r2) => r1 !== r2
});
this.dataSource = ds.cloneWithRows(this.props.images);
}
renderRow(rowData){
const {uri} = rowData;
return(
<Image style={styles.imageSize} source={{uri: uri}} />
)
}
render() {
return (
<View>
<ListView
contentContainerStyle={styles.list}
dataSource={this.dataSource}
renderRow={this.renderRow}
/>
</View>
);
}
}
var styles = StyleSheet.create({
imageSize: {
//newWidth is the width of the device divided by 4.
//This is so that four images will display in each row.
width: newWidth,
height: newWidth,
padding: 2
},
list: {
flexDirection: 'row',
flexWrap: 'wrap'
}
});
Run Code Online (Sandbox Code Playgroud)
如何添加填充并使我的所有图像正确显示?
这是我希望我的应用显示图像的图像。
但是,当我将renderRow函数的返回内容包装在带有填充的a中时,它的显示方式如下。有填充是我想要的,但是仅显示前3张图片。我希望显示所有带有填充的9张图片。
通过使用 Thomas 在评论部分提供的建议,我能够解决我的问题。
我所做的是添加 alignSelf: 'flex-start'
下面是代码的样子:
renderRow(rowData){
const {uri} = rowData;
return(
<View style={{padding: 1, alignSelf: 'flex-start'}}>
<Image style={styles.imageSize} source={{uri: uri}} />
</View>
)
}
Run Code Online (Sandbox Code Playgroud)
图片现在可以正确显示。