css - 视图中的中心元素
问题描述
我有一个带有一些 TextInput 子组件的表单组件,如下所示:
const FormComponent = () => {
return (
<View style={StyleSheet.container}>
<TextInput1/>
<TextInput2/>
<TextInput3/>
</View>
)
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: "center",
justifyContent: "center",
},
});
当我尝试使用 StyleSheet 居中对齐表单组件时,似乎什么也没发生。即,文本输入保留在屏幕的左上角。
但是,如果我将样式直接应用于视图,如下所示:
<View style={{ flex: 1, justifyContent: "center", alignItems: "center" }}>
然后 textinput 组件在屏幕上居中对齐。
我不明白为什么会发生这种情况,如果可能的话,我想通过使用样式表来保持代码干净。
解决方案
使用样式而不是 StyleSheet ,这是一个错字
<View style={styles.container}>
<TextInput1/>
<TextInput2/>
<TextInput3/>
</View>
推荐阅读
- redux - 带有条件语句的 Redux useSelector
- vim - Coc:从键或脚本更改设置
- docker - dockerize NestJs 项目仅使用构建文件
- storybook - 是否可以在 React Storybook 中访问真实的 API
- redux - Redux Thunk 未调度
- python - 核心配置文件 OpenGL 没有绘制任何东西
- javascript - 如何在 app.component 中检测当前在 Angular 中处于活动状态的父布局组件?
- java - Gradle 测试 - 为失败的测试打印标准输出/标准错误
- ios - iOS 自定义键盘高度启动故障
- javascript - 如何使用 image-picker.js 将图像设为默认值?