我已经阅读了几个flexbox教程,但我仍然无法完成这个简单的任务 .
如何将红色框设为100%宽度?
码:
<View style={styles.container}>
<Text style={styles.welcome}>
Welcome to React Natives
</Text>
<Text style={styles.line1}>
line1
</Text>
<Text style={styles.instructions}>
Press Cmd+R to reload,{'\n'}
Cmd+D or shake for dev menu
</Text>
</View>
样式:
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
borderWidth: 1,
flexDirection: 'column',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
borderWidth: 1,
},
line1: {
backgroundColor: '#FDD7E4',
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
borderWidth: 1,
},
谢谢!
Update 1: Nishanth Shankar的建议,为包装器添加flex:1,flexDirection:'row'
输出:
码:
<View style={styles.container}>
<View style={{flex:1}}>
<Text style={styles.welcome}>
Welcome to React Natives
</Text>
</View>
<View style={{flex:1}}>
<Text style={styles.line1}>
line1
</Text>
</View>
<View style={{flex:1}}>
<Text style={styles.instructions}>
Press Cmd+R to reload,{'\n'}
Cmd+D or shake for dev menu
</Text>
</View>
</View>
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
borderWidth: 1,
flexDirection: 'row',
flexWrap: 'wrap',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
borderWidth: 1,
},
line1: {
backgroundColor: '#FDD7E4',
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
borderWidth: 1,
},
7 回答
只需将
alignSelf: "stretch"
添加到项目的样式表中即可 .你应该使用Dimensions
首先,定义Dimensions .
然后,改变
line1
样式,如下所示:Editted:
为了仅展示中心文本,可以采取不同的方法 - Unflex the other views.
让flexDirection保持'column'
从容器中删除
alignItems : 'center'
将
alignSelf:'center'
添加到您不想弯曲的文本视图中您可以将Text组件包装在View组件中,并将View的flex设置为1 .
flex将给出:
如果style.container中的
flexDirection:'row'
,则为100%宽度如果style.container中的
flexDirection:'column'
,则为100%高度使用javascript获取宽度和高度,并以View的样式添加它们 . 要获得完整的宽度和高度,请使用
Dimensions.get('window').width
https://facebook.github.io/react-native/docs/dimensions.html然后,
首先添加Dimension组件:
第二个定义变量:
第三个把它放在你的样式表中:
实际上在这个例子中我想做出响应式视图,并且只想查看0.25的屏幕视图,所以我将它乘以0.25,如果你想要100%的屏幕不要将它与任何这样的东西相乘:
Here you go:
只需更改line1样式,如下所示: