react-native - 为什么视图会自动占用 100% 的宽度?
问题描述
我试图将一个视图包含在另一个视图中,但是即使我没有指定它,封闭的视图也占用了 100% 的宽度。
我的主文件
import React from "react";
import { Text, View, Image } from "react-native";
import style from "../styles/screens/RegisterStyle";
export default function SignupRegisterScreen(props) {
return (
<View style={style.container}>
<View style={style.step}>
<Text style={style.stepText}>Step-1</Text>
</View>
</View>
);
}
我的风格
import { StyleSheet } from "react-native";
import { scaleSize, scaleFont } from "../../theme/Mixins";
const style = StyleSheet.create({
container: {
backgroundColor: "#CBF4E7",
flex: 1,
},
step: {
backgroundColor: "white",
paddingHorizontal: scaleSize(28),
paddingVertical: scaleSize(4),
borderRadius: scaleSize(20),
marginTop: scaleSize(30),
},
stepText: {
fontSize: scaleFont(16),
lineHeight: scaleFont(19),
},
});
export default style;
如何使视图仅占用必要的宽度?
解决方案
这是因为你的文字。因此,如果您删除<Text style={style.stepText}>Step-1</Text>
. 宽度将设置为 0。所以Text
组件影响父视图宽度的原因是因为您没有为父视图指定高度或宽度。所以 react native 会自动使用它的子容器Text
来填充它的高度和宽度。所以,如果你增加文本的字体大小,父容器的高度就会增加。
推荐阅读
- sql - 如何在 JSON 数组中循环子列表
- gnuplot - 如何通过字符串定义颜色名称以在 linecolor lc 中使用,不工作 gnuplot
- python - Python根据输入执行函数中的某些步骤
- java - spring boot - 在 oracle 数据库上回滚
- python - 从异步函数监视 multiprocessing.pool.Pool 对象
- xamarin - 滚动页面时隐藏 TopTabs
- c# - 为什么 PreviewMouseUp 在 PreviewMouseDown 之前触发并快速拖动
- compilation - Fortran 编译错误:意外元素
- swift - Parse-Server 文件 URL 没有改变
- spring-boot - 将自定义异常从 Kotlin Webclient awaitExchange Lambda 函数传递到 Web 控制器的正确方法是什么?