【发布时间】:2016-01-12 21:01:00
【问题描述】:
在 React Native 中使用 flexbox 我在尝试执行以下看似简单的任务时遇到了很多麻烦:
我在一个容器中有两个项目。我想要一个居中,另一个直接位于它的左侧。
【问题讨论】:
标签: flexbox react-native
在 React Native 中使用 flexbox 我在尝试执行以下看似简单的任务时遇到了很多麻烦:
我在一个容器中有两个项目。我想要一个居中,另一个直接位于它的左侧。
【问题讨论】:
标签: flexbox react-native
你可以改变两个外部组件的flex size属性,只要它们相同,中间的item就会居中。
注意第一个组件的 justifyContent:'flex-end' 属性。这种样式使项目直接位于中间项目的左侧。
<View style={styles.container}>
<View style={styles.component1}>
<Text>Right</Text>
</View>
<View style={styles.component2}>
<Text style={{ textAlign:'center' }}>I am the Center Component</Text>
</View>
<View style={styles.component3}></View>
</View>
container: {
flexDirection:'row',
flex:1,
marginTop:60
},
component1: {
flex:1,
flexDirection: 'row',
justifyContent: 'flex-end',
height:80
},
component2: {
flex:1,
height:80,
backgroundColor: 'red',
justifyContent:'center'
},
component3: {
flex:1,
}
另外,整个代码如下:
'use strict';
var React = require('react-native');
var {
AppRegistry,
StyleSheet,
Text,
View,
} = React;
var SampleApp = React.createClass({
render: function() {
return (
<View style={styles.container}>
<View style={styles.component1}>
<Text>Right</Text>
</View>
<View style={styles.component2}>
<Text style={{ textAlign:'center' }}>I am the Center Component</Text>
</View>
<View style={styles.component3}></View>
</View>
);
}
});
var styles = StyleSheet.create({
container: {
flexDirection:'row',
flex:1,
marginTop:60
},
component1: {
flex:1,
flexDirection: 'row',
justifyContent: 'flex-end',
height:80
},
component2: {
flex:1,
height:80,
backgroundColor: 'red',
justifyContent:'center'
},
component3: {
flex:1,
}
});
AppRegistry.registerComponent('SampleApp', () => SampleApp);
【讨论】:
https://rnplay.org/apps/phsgpw 链接,因为它们永远不会起作用