javascript - 如何在 React Native 中垂直居中项目

标签 javascript reactjs react-native web-frontend react-native-web

免责声明:这是一个有关学校项目的问题。

我正在开发一个使用 React Native 作为前端的全栈项目。我在页面布局方面遇到一些问题。这是我的代码:

应用程序.js:

import React, {useState} from 'react';
import { StyleSheet, Text, View, Switch } from 'react-native';

import Header from "./components/Header";

export default function App() {
  const [isEnabled, setIsEnabled] = useState(false);
  const toggleSwitch = () => setIsEnabled(previousState => !previousState);
  return (
    <View style={styles.screen}>
      <Header title="Web Application" />
      <Text style={styles.text}>Example Text</Text>
      <Text style={styles.text}>Not much here yet, so play with this switch</Text>
      <Switch
        style={styles.switch}
        trackColor={{ false: "#767577", true: "#81b0ff" }}
        thumbColor={isEnabled ? "#f5dd4b" : "#f4f3f4"}
        ios_backgroundColor="#3e3e3e"
        onValueChange={toggleSwitch}
        value={isEnabled}
      />
    </View>
  );
}

const styles = StyleSheet.create({
  screen: {
    flex: 1,
    alignItems: "center",
    justifyContent: "center"
  },
  text: {
    flex: 1,
    textAlign: 'center',
    fontWeight: 'bold',
    fontSize: 24,
    backgroundColor: 'white'
  },
  switch: {
    alignSelf: 'center',
    flexDirection: 'column',
    justifyContent: 'space-between'
  }
});
header.js

import React from "react";
import { StyleSheet, View, Text, Platform } from "react-native";

import Colors from "../constants/colors";

const Header = props => {
  return (
    <View style={{ ...styles.headerBase, ...Platform.select({ios: styles.headerIOS, android: styles.headerAndroid, web: styles.headerBase})}} >
      <Text style={styles.headerTitle}>{props.title}</Text>
    </View>
  );
}

const styles = StyleSheet.create({
  headerBase: {
    width: "100%",
    height: 90,
    paddingTop: 36,
    alignItems: "center",
    justifyContent: "center",

  },
  headerIOS: {
    backgroundColor: 'black',
    borderBottomColor: '#ccc',
    borderBottomWidth: 1,
  },
  headerAndroid: {
    backgroundColor: Colors.primary,
  },
  headerTitle: {
    color: Platform.OS === 'ios' ? Colors.primary : 'black',
    fontSize: 24
  },
});

export default Header;

我想要做的是将标题放在顶部,然后是文本,然后居中(垂直和水平),我希望有开关。相反,此代码当前正确放置标题和文本,但开关放置在最底部。任何帮助/资源将不胜感激。

最佳答案

尝试在文本样式上不使用 flex: 1

import React, {useState} from 'react';
import { StyleSheet, Text, View, Switch } from 'react-native';

import Header from "./components/Header";

export default function App() {
  const [isEnabled, setIsEnabled] = useState(false);
  const toggleSwitch = () => setIsEnabled(previousState => !previousState);
  return (
    <View style={styles.screen}>
      <Header title="Web Application" />
      <Text style={styles.text}>Example Text</Text>
      <Text style={styles.text}>Not much here yet, so play with this switch</Text>
      <Switch
        style={styles.switch}
        trackColor={{ false: "#767577", true: "#81b0ff" }}
        thumbColor={isEnabled ? "#f5dd4b" : "#f4f3f4"}
        ios_backgroundColor="#3e3e3e"
        onValueChange={toggleSwitch}
        value={isEnabled}
      />
    </View>
  );
}

const styles = StyleSheet.create({
  screen: {
    flex: 1,
    alignItems: "center",
    justifyContent: "center"
  },
  text: {
    textAlign: 'center',
    fontWeight: 'bold',
    fontSize: 24,
    backgroundColor: 'white'
  },
  switch: {
    alignSelf: 'center',
    flexDirection: 'column',
    justifyContent: 'space-between'
  }
});

这是documentation .

关于javascript - 如何在 React Native 中垂直居中项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60996791/

相关文章:

php - ajax问题(页面刷新)

javascript - 使用meteor-admin-lte无法获得完整高度

javascript - HTML5 进度元素

react-native - react native 模式未打开

javascript - 在类方法中测试静态方法调用时出现TypeError

javascript - Angular2 http重试逻辑

javascript - 如何在 React 中编辑特定对象

javascript - 当我使用 axios POST 时,Req.body 为空,但当我使用 'request' 时,它工作正常

javascript - React 将 props 向下传递给所有后代组件

javascript - touchableopacity onpress 函数未定义(不是函数)React Native