【发布时间】:2015-06-02 08:01:38
【问题描述】:
我可以给出样式数值的高度元素,例如40,但这些必须是整数。如何使我的组件具有100% 的高度?
【问题讨论】:
标签: react-native
我可以给出样式数值的高度元素,例如40,但这些必须是整数。如何使我的组件具有100% 的高度?
【问题讨论】:
标签: react-native
我使用的是 ScrollView,所以这些解决方案都没有解决我的问题。直到我在滚动视图上尝试了 contentContainerStyle={{flexGrow: 1}} 道具。似乎没有它-scrollviews 将始终与它们的内容一样高。
我的解决方案在这里找到:React native, children of ScrollView wont fill full height
【讨论】:
大部分时间应该使用flexGrow: 1 或flex: 1
或者你可以使用
import { Dimensions } from 'react-native';
const { Height } = Dimensions.get('window');
styleSheet({
classA: {
height: Height - 40,
},
});
如果它们都不适合你试试:
container: {
position: 'absolute',
top: 0,
bottom: 0,
left: 0,
right: 0,
}
【讨论】:
<View style={styles.container}>
</View>
const styles = StyleSheet.create({
container: {
flex: 1
}
})
【讨论】:
flex:1 几乎适用于任何情况。但是,请记住对于ScrollView,控制视图高度的是contentContainerStyle:
错误
const styles = StyleSheet.create({
outer: {
flex: 1,
},
inner: {
flex: 1
}
});
<ScrollView style={styles.outer}>
<View style={styles.inner}>
</View>
</ScrollView>
正确
const styles = StyleSheet.create({
outer: {
flex: 1,
},
inner: {
flex: 1
}
});
<ScrollView contentContainerStyle={styles.outer}>
<View style={styles.inner}>
</View>
</ScrollView>
【讨论】:
contentContainerStyle。这为我解决了这个问题:-)
试试这个:
<View style={{flex: 1}}>
<View style={{flex: 1, backgroundColor: 'skyblue'}} />
</View>
您可以在 react-native 在线文档 (https://facebook.github.io/react-native/docs/height-and-width) 中获得更多帮助。
【讨论】:
将窗口高度放入一个变量中,然后将其指定为您要定位的 flex 容器的高度:
let ScreenHeight = Dimensions.get("window").height;
按照你的风格:
var Styles = StyleSheet.create({ ... height: ScreenHeight });
注意,使用前必须先导入Dimensions:
import { ... Dimensions } from 'react-native'
【讨论】:
您可以简单地将height: '100%' 添加到您项目的样式表中。
它对我有用
【讨论】:
查看flexbox doc。在样式表中,使用:
flex:1,
【讨论】:
flex-direction了吗?
flex 选项来增加元素。