css - react-native 中的宽度和高度
问题描述
请解释形成元素样式的原则视图大小。我学习了 FlexBox,但它打破了我的所有理解。
代码
export const App = () => {
return (
<SafeAreaView>
<View style={styles.container}>
<View style={styles.element}>
<View style={styles.element1}></View>
</View>
</View>
</SafeAreaView>
);
};
const styles = StyleSheet.create({
container: {
width: '100%',
height: '100%',
backgroundColor: 'green',
},
element: {
// width: '100%',
// height: '100%',
backgroundColor: 'yellow',
// top: '5%',
},
element1: {
width: '50%',
height: '80%',
backgroundColor: 'red',
},
});
解决方案
您可以使用flex: 1
而不是
width: '100%', height: '100%',
.
这Flex: 1
意味着组件(如果是视图)的优先级值为1
,这意味着,如果在同一个视图上没有另一个组件flex: 1
,则该组件将占据整个屏幕。如果有两个组件 flex: 1
,每个组件将占据整个屏幕的 50%。
这是文档:React native flexbox
推荐阅读
- r - 当标签太长而无法放入绘图时,几何标签会排斥
- python-3.x - Python 中 AWS Lambda 函数中的 Runtime.HandlerNotFound 错误
- databricks - Databricks Community Edition 冲突的 Scala 库或附加到集群的 JAR
- laravel - 在注册系统中使用 laravel 发送电子邮件
- sql-server - SQL Server 链接服务器未返回所有结果
- reactjs - React Native 如何设置启动声音
- sql - SQL:将行插入一个表中的多个值
- java - 在heroku中部署一个带有资源文件的jar文件
- excel - 用颜色高亮 3*3 块对角单元格
- python - 'If not' 避免零除法