react-native - 在 React Native 中使用 flex 进行网格布局
问题描述
我正在使用 flex 来显示一些配置文件。
<View style={styles.flexContainer}>
<View style={styles.profileImgContainer} >
<Image style={styles.profileImg}/>
</View>
<View style={styles.name}>
<Markdown>{user.name}</Markdown>
</View>
</View>
const styles = StyleSheet.create({
flexContainer: {
flexDirection: 'row'
},
...,
});
我想使用 row1 和 row2 将 Image 放在 col1 的中心。我想将 Markdown 放在 row1 的 col2 中。
我的 flex 容器应该是这样的。
col1 | col2 |
row1 |______|
| |
row2 ____|______|
我怎么能做到这一点?
解决方案
输出:
代码:
<View style={styles.container}>
<View style={{flex: 1, flexDirection: 'row'}}>
<View style={styles.col1}>
<Image source={{uri: 'https://www.deine-tierwelt.de/magazin/wp-content/uploads/sites/2/2013/10/Australian-Cattle-Dog-1000x570.jpg'}} style={{width: 100, height: 100}} />
</View>
<View style={styles.col2}>
<View style={styles.row1}>
<Text> Username </Text>
</View>
<View style={styles.row2}>
</View>
</View>
</View>
<View style={{flex: 1}} />
</View>
样式表:
container: {
flex: 1,
justifyContent: 'center',
paddingTop: Constants.statusBarHeight,
backgroundColor: '#ecf0f1',
flexDirection: 'column',
padding: 8,
},
col1: {
flex: 1,
flexDirection: 'column',
justifyContent: 'center',
alignItems: 'center',
backgroundColor: 'gray'
},
col2: {
flex:1,
flexDirection: 'column'
},
row1: {
flex: 1,
flexDirection: 'row',
justifyContent: 'center',
backgroundColor: 'lightgray'
},
row2: {
flex: 1,
flexDirection: 'row',
justifyContent: 'center',
backgroundColor: 'darkgray'
}
演示:
推荐阅读
- html - Bootstrap 5 Justify Content Center + Container Fluid Causing Unexpected Behavior
- python - python - 如何在plotly Python中相对于x刻度对齐垂直条?
- android - Flutter - DEBUG 中的 Webview 正在工作,但在 RELEASE apk 上显示为空白,为什么?
- php - 如何在 php 函数中“链接”两个对象(如关键字及其同义词)?
- apache-kafka - 使用 Pentaho 中 Kafka 主题的参数咨询 SQL
- go - 如何使用 context.WithValue() 存储 context.CancelFunc 类型的值?
- javascript - 在从数组 Vue.js/Nuxt 中选择一个项目之前尝试翻译字符串
- r - 是否可以修改 mlr3 中预定义学习器的代码?
- java - ElementReferenceException:过时的元素引用:元素未附加到页面文档
- javascript - Angular Storybook - 有没有办法自动生成 .stories.ts 文件