【发布时间】:2019-07-10 13:08:49
【问题描述】:
如何在我的相机胶卷中完美添加margin: 1?因为当我在<Image .../> 中添加margin: 1 时,顺序将不正确。
我想要的结果就像在 Instagram 中一样。左右两边没有边距吧?
就像这张图片:
这是我的代码:
render() {
return(
<View style={styles.container}>
{/* <SelectedImage source={{uri: this.state.pickedImage}}/> */}
<Image source={{uri: this.state.pickedImage}} style={styles.image}/>
<ScrollView contentContainerStyle={styles.scrollView} showsVerticalScrollIndicator={false}>
{this.state.photos.map((photos, index) => {
return(
<TouchableHighlight
style={{opacity: index === this.state.index ? .5 : 1}}
onPress={() => this.setState({pickedImage: photos.node.image.uri})}
key={index}
underlayColor='transparent'
>
<Image
style={{width: width / 3, height: width /3}}
source={{uri: photos.node.image.uri}}
resizeMode='cover'
/>
</TouchableHighlight>
);
})}
</ScrollView>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
height: '100%',
width: '100%',
justifyContent: 'center',
alignItems: 'center',
backgroundColor: 'white'
},
scrollView: {
flexWrap: 'wrap',
flexDirection: 'row'
},
scrollViewContainer: {
flex: 1,
flexDirection: 'row',
},
image: {
width: '100%',
height: 300,
backgroundColor: 'black'
}
});
这是我自己的结果:(不要打扰我只是没有完美裁剪的黑色边框,我的 Android 模拟器中只有 6 张照片)。
建议答案:
componentDidMount() {
this.getPhotos();
this.rightButtonIcon();
requestExternalStoragePermission();
photoFilter = () => {
var arr = this.state.photos;
var number = 0;
arr.forEach((item) => {
switch(number) {
case 0:
number = 1;
item.position="left"
break;
case 1:
number = 2;
item.position="center"
break;
case 2:
number = 0;
item.position="right"
break;
default:
return null;
}
})
this.setState({photos: arr})
}
};
imageStylePositionCalc = (pos) =>{
if(pos==="left") return {marginRight:1,marginBottom:1}
if(pos==="center") return {marginRight:1,marginBottom:1}
if(pos==="right") return {marginBottom:1}
}
render() {
return(
<View style={styles.container}>
{/* <SelectedImage source={{uri: this.state.pickedImage}}/> */}
<Image source={{uri: this.state.pickedImage}} style={styles.image}/>
<ScrollView contentContainerStyle={styles.scrollView} showsVerticalScrollIndicator={false}>
{this.state.photos.map((photos, index) => {
return(
<TouchableHighlight
style={{opacity: index === this.state.index ? .5 : 1}}
onPress={() => this.setState({pickedImage: photos.node.image.uri})}
key={index}
underlayColor='transparent'
>
<Image
style={[{width: width / 3, height: width /3}, this.imageStylePositionCalc(photos.position)]}
source={{uri: photos.node.image.uri}}
resizeMode='cover'
/>
</TouchableHighlight>
);
})}
</ScrollView>
</View>
);
}
}
这是另一个问题,margin: 1 占据了屏幕的整个宽度,第二行没有margin:
【问题讨论】:
-
您是否尝试将
borderColor:"#fff"添加到图像(或touchableOpacity)? -
不,我还没试过。
-
正如我所提到的,我不希望第一列在左侧有边距,第三列我不希望在右侧有边距。
-
如何渲染图像?每行是否有 2 个,或者它们可以像您发布的图片一样具有随机尺寸?
-
当前图像是什么样的?请给我看结果屏幕。
标签: react-native stylesheet camera-roll