gpt4 book ai didi

reactjs - React Native iOS 应用程序中的两列布局

转载 作者:行者123 更新时间:2023-12-05 07:43:42 24 4
gpt4 key购买 nike

在我的 ReactNative 应用程序中,我试图为一组项目实现两列布局。

我正在处理的代码:

<Content>
<Grid style={{flexWrap: 'wrap', flex: 1}}>
{this.state.stories.map(function(story, index){
if (index % 2 === 0) {
return <Col key={ index } style={{margin: 5, width: Dimensions.get('window').width / 2.2}}>
<View>
<Image style={{ height: 250, borderRadius: 10}} source={{uri: story.picture_url}} />
</View>
</Col>
} else{
return <Col key={ index } style={{margin: 5, width: Dimensions.get('window').width / 2.2}}>
<View style={{marginTop: 25}}>
<Image style={{height: 250, borderRadius: 10}} source={{uri: story.picture_url}} />
</View>
</Col>
}
}.bind(this))
}
</Grid>
</Content>

我做错了什么?

我想要的是:
What i want is

我得到的是:
What i'm getting is

最佳答案

完全不清楚您的组件是做什么的,但总的来说我强烈建议使用 flexbox用于布局,而不是尝试手动计算列的样式。这是一个使用 react-native init 的人为示例,说明如何实现两列布局。

/**
* Sample React Native App
* https://github.com/facebook/react-native
* @flow
*/

import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View
} from 'react-native';

export default class layout extends Component {
render() {
return (
<View style={styles.container}>
<View style={styles.column}>
<View style={[styles.box, {backgroundColor:'red'}]}/>
<View style={[styles.box, {backgroundColor:'pink'}]}/>
<View style={[styles.box, {backgroundColor:'orange'}]}/>
</View>
<View style={styles.space_between_columns}/>
<View style={styles.column}>
<View style={[styles.box, {backgroundColor:'blue'}]}/>
<View style={[styles.box, {backgroundColor:'green'}]}/>
<View style={[styles.box, {backgroundColor:'purple'}]}/>
</View>
</View>
);
}
}

const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection:'row',
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
column: {
flexDirection:'column',
justifyContent:'space-between',
alignItems:'center',
height:200,
width:50
},
space_between_columns:{
width:100
},
box: {
height:50,
width:50
}
});

AppRegistry.registerComponent('layout', () => layout);

关于reactjs - React Native iOS 应用程序中的两列布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43523775/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com