React Native Flexbox 中的 100% 宽度
100% width in React Native Flexbox
我已经阅读了几个 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,
},
谢谢!
更新 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,
},
已编辑:
为了仅弯曲中心文本,可以采取不同的方法 - 取消弯曲其他视图。
- 让flexDirection保持在'column'
- 从容器中删除
alignItems : 'center'
- 将
alignSelf:'center'
添加到您不想弯曲的文本视图中
您可以将 Text 组件包裹在 View 组件中,并为 View 赋予 1 的 flex。
flex 将给出:
如果 styles.container
中的 flexDirection:'row'
宽度为 100%
如果 styles.container
中的 flexDirection:'column'
高度为 100%
只需将 alignSelf: "stretch"
添加到您项目的样式表中即可。
line1: {
backgroundColor: '#FDD7E4',
alignSelf: 'stretch',
textAlign: 'center',
},
你应该使用Dimensions
首先,定义维度。
import { Dimensions } from "react-native";
var width = Dimensions.get('window').width; //full width
var height = Dimensions.get('window').height; //full height
然后,更改 line1
样式如下:
line1: {
backgroundColor: '#FDD7E4',
width: width,
},
使用javascript获取宽高,添加到View的样式中。
要获得完整的宽度和高度,请使用 Dimensions.get('window').width
https://facebook.github.io/react-native/docs/dimensions.html
getSize() {
return {
width: Dimensions.get('window').width,
height: Dimensions.get('window').height
}
}
然后,
<View style={[styles.overlay, this.getSize()]}>
首先添加维度组件:
import { AppRegistry, Text, View,Dimensions } from 'react-native';
第二次定义变量:
var height = Dimensions.get('window').height;
var width = Dimensions.get('window').width;
第三次将其放入您的样式表中:
textOutputView: {
flexDirection:'row',
paddingTop:20,
borderWidth:1,
borderColor:'red',
height:height*0.25,
backgroundColor:'darkgrey',
justifyContent:'flex-end'
}
实际上在这个例子中我想制作响应式视图并且只想查看屏幕视图的 0.25 所以我将它乘以 0.25,如果你想要 100% 的屏幕不要将它与任何类似的东西相乘:
textOutputView: {
flexDirection:'row',
paddingTop:20,
borderWidth:1,
borderColor:'red',
height:height,
backgroundColor:'darkgrey',
justifyContent:'flex-end'
}
Noted: Try to fully understanding about flex concept.
<View style={{
flex: 2,
justifyContent: 'center',
alignItems: 'center'
}}>
<View style ={{
flex: 1,
alignItems: 'center,
height: 50,
borderWidth: 1,
borderColor: '#000'
}}>
<Text>Welcome to React Nativ</Text>
</View>
<View style={{
flex: 1,
alignItems: 'center,
borderWidth: 1,
borderColor: 'red ',
height: 50
}}
>
<Text> line 1 </Text>
</View>
<View style={{
flex: 1,
alignItems: 'center,
height: 50,
borderWidth: 1,
borderColor: '#000'
}}>
<Text>
Press Cmd+R to reload,{'\n'}
Cmd+D or shake for dev menu
</Text>
</View>
</View>
给你:
只需按照以下方式更改 line1 样式即可:
line1: {
backgroundColor: '#FDD7E4',
width:'100%',
alignSelf:'center'
}
只需删除容器样式中的 alignItems: 'center'
并将 textAlign: "center"
添加到 line1
样式,如下所示。
它会很好用
container: {
flex: 1,
justifyContent: 'center',
backgroundColor: '#F5FCFF',
borderWidth: 1,
}
line1: {
backgroundColor: '#FDD7E4',
textAlign:'center'
},
Style ={{width : "100%"}}
试试这个:
StyleSheet generated: {
"width": "80%",
"textAlign": "center",
"marginTop": 21.8625,
"fontWeight": "bold",
"fontSize": 16,
"color": "rgb(24, 24, 24)",
"fontFamily": "Trajan Pro",
"textShadowColor": "rgba(255, 255, 255, 0.2)",
"textShadowOffset": {
"width": 0,
"height": 0.5
}
}
width: '100%'
和 alignSelf: 'stretch'
对我不起作用。 Dimensions
不适合我的任务,因为我需要在深度嵌套的视图上进行操作。如果我重写您的代码,这对我有用。我刚刚添加了一些 View
s 并使用 flex
属性来实现所需的布局:
{/* a column */}
<View style={styles.container}>
{/* some rows here */}
<Text style={styles.welcome}>
Welcome to React Natives
</Text>
{/* this row should take all available width */}
<View style={{ flexDirection: 'row' }}>
{/* flex 1 makes the view take all available width */}
<View style={{ flex: 1 }}>
<Text style={styles.line1}>
line1
</Text>
</View>
{/* I also had a button here, to the right of the text */}
</View>
{/* the rest of the rows */}
<Text style={styles.instructions}>
Press Cmd+R to reload,{'\n'}
Cmd+D or shake for dev menu
</Text>
</View>
只需使用宽度:'100%'
line1: {
backgroundColor: '#FDD7E4',
width: '100%'
},
我已经阅读了几个 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,
},
谢谢!
更新 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,
},
已编辑:
为了仅弯曲中心文本,可以采取不同的方法 - 取消弯曲其他视图。
- 让flexDirection保持在'column'
- 从容器中删除
alignItems : 'center'
- 将
alignSelf:'center'
添加到您不想弯曲的文本视图中
您可以将 Text 组件包裹在 View 组件中,并为 View 赋予 1 的 flex。
flex 将给出:
如果 styles.container
中的flexDirection:'row'
宽度为 100%
如果 styles.container
中的flexDirection:'column'
高度为 100%
只需将 alignSelf: "stretch"
添加到您项目的样式表中即可。
line1: {
backgroundColor: '#FDD7E4',
alignSelf: 'stretch',
textAlign: 'center',
},
你应该使用Dimensions
首先,定义维度。
import { Dimensions } from "react-native";
var width = Dimensions.get('window').width; //full width
var height = Dimensions.get('window').height; //full height
然后,更改 line1
样式如下:
line1: {
backgroundColor: '#FDD7E4',
width: width,
},
使用javascript获取宽高,添加到View的样式中。
要获得完整的宽度和高度,请使用 Dimensions.get('window').width
https://facebook.github.io/react-native/docs/dimensions.html
getSize() {
return {
width: Dimensions.get('window').width,
height: Dimensions.get('window').height
}
}
然后,
<View style={[styles.overlay, this.getSize()]}>
首先添加维度组件:
import { AppRegistry, Text, View,Dimensions } from 'react-native';
第二次定义变量:
var height = Dimensions.get('window').height;
var width = Dimensions.get('window').width;
第三次将其放入您的样式表中:
textOutputView: {
flexDirection:'row',
paddingTop:20,
borderWidth:1,
borderColor:'red',
height:height*0.25,
backgroundColor:'darkgrey',
justifyContent:'flex-end'
}
实际上在这个例子中我想制作响应式视图并且只想查看屏幕视图的 0.25 所以我将它乘以 0.25,如果你想要 100% 的屏幕不要将它与任何类似的东西相乘:
textOutputView: {
flexDirection:'row',
paddingTop:20,
borderWidth:1,
borderColor:'red',
height:height,
backgroundColor:'darkgrey',
justifyContent:'flex-end'
}
Noted: Try to fully understanding about flex concept.
<View style={{
flex: 2,
justifyContent: 'center',
alignItems: 'center'
}}>
<View style ={{
flex: 1,
alignItems: 'center,
height: 50,
borderWidth: 1,
borderColor: '#000'
}}>
<Text>Welcome to React Nativ</Text>
</View>
<View style={{
flex: 1,
alignItems: 'center,
borderWidth: 1,
borderColor: 'red ',
height: 50
}}
>
<Text> line 1 </Text>
</View>
<View style={{
flex: 1,
alignItems: 'center,
height: 50,
borderWidth: 1,
borderColor: '#000'
}}>
<Text>
Press Cmd+R to reload,{'\n'}
Cmd+D or shake for dev menu
</Text>
</View>
</View>
给你:
只需按照以下方式更改 line1 样式即可:
line1: {
backgroundColor: '#FDD7E4',
width:'100%',
alignSelf:'center'
}
只需删除容器样式中的 alignItems: 'center'
并将 textAlign: "center"
添加到 line1
样式,如下所示。
它会很好用
container: {
flex: 1,
justifyContent: 'center',
backgroundColor: '#F5FCFF',
borderWidth: 1,
}
line1: {
backgroundColor: '#FDD7E4',
textAlign:'center'
},
Style ={{width : "100%"}}
试试这个:
StyleSheet generated: {
"width": "80%",
"textAlign": "center",
"marginTop": 21.8625,
"fontWeight": "bold",
"fontSize": 16,
"color": "rgb(24, 24, 24)",
"fontFamily": "Trajan Pro",
"textShadowColor": "rgba(255, 255, 255, 0.2)",
"textShadowOffset": {
"width": 0,
"height": 0.5
}
}
width: '100%'
和 alignSelf: 'stretch'
对我不起作用。 Dimensions
不适合我的任务,因为我需要在深度嵌套的视图上进行操作。如果我重写您的代码,这对我有用。我刚刚添加了一些 View
s 并使用 flex
属性来实现所需的布局:
{/* a column */}
<View style={styles.container}>
{/* some rows here */}
<Text style={styles.welcome}>
Welcome to React Natives
</Text>
{/* this row should take all available width */}
<View style={{ flexDirection: 'row' }}>
{/* flex 1 makes the view take all available width */}
<View style={{ flex: 1 }}>
<Text style={styles.line1}>
line1
</Text>
</View>
{/* I also had a button here, to the right of the text */}
</View>
{/* the rest of the rows */}
<Text style={styles.instructions}>
Press Cmd+R to reload,{'\n'}
Cmd+D or shake for dev menu
</Text>
</View>
只需使用宽度:'100%'
line1: {
backgroundColor: '#FDD7E4',
width: '100%'
},