【问题标题】:How to vertically and horizontally align child components 'on top' of parent in React Native?如何在 React Native 中垂直和水平对齐父组件“顶部”的子组件?
【发布时间】:2020-03-21 14:19:18
【问题描述】:

在 React Native 中让两个子组件在其父元素的“顶部”垂直和水平对齐时遇到很多麻烦。

我正在尝试将Loader 和Button 放在RTCView 之上。

我目前有这个 JSX 在父级中返回:

  if (localStream) {
    return (
      <View style={styles.videoViewWrapper}>
        <RTCView style={styles.android} streamURL={localStream.toURL()} />
        <View
          style={{ justifyContent: 'center', position: 'absolute', }}
        >
          <Loader
            title={callDetails}
          >
          </Loader>
          <Button
            mode="contained"
            style={{ width: 150, margin: 100 }}
            onPress={handleCancelCall}>
            Cancel
          </Button>
        </View>
      </View>
    );
  }

还有这个Loader:

Loader = props => {
  return (
    <>
      <StatusBar
        hidden
      />
      <View style={{
        flex: 1,
        alignItems: 'center',
      }}>
        <Text
          style={styles.text}
        >
          {props.title}
        </Text>
        <ProgressBar color={Colors.green800} indeterminate={true} style={{ width: 100, height: 1 }} />
      </View>
    </>
  )
}

const styles = StyleSheet.create({
  text: {
    fontFamily: "Quicksand-Light",
    fontSize: 22,
    textAlign: "center",
    color: "#333333",
    marginBottom: 25,
    justifyContent: 'center',
  }
});

虽然我已经让Loader 显示在其父级的“顶部”,但我无法将它们移动到屏幕顶部的位置。

任何帮助将不胜感激!

【问题讨论】:

  • 你能张贴当前情况的屏幕截图并指出它们应该在哪里吗?谢谢

标签: javascript react-native react-native-stylesheet


【解决方案1】:

您的绝对定位视图需要覆盖其父级。您可以通过在其样式中添加top: 0, left: 0, right: 0, bottom: 0 来实现此目的

您可能需要编辑按钮样式。我从您的代码中删除了它,如果您需要边距或特定宽度,请添加它。

<View style={{ justifyContent: 'center', alignItems: 'center', position: 'absolute', top: 0, left: 0, right: 0, bottom: 0 }}>
   <Loader title={callDetails} />
   <Button mode="contained" onPress={handleCancelCall}>Cancel</Button>
</View>

【讨论】:

  • 正确!!特维姆。
猜你喜欢
  • 2021-05-01
  • 2021-09-23
  • 2020-10-13
  • 2015-06-13
  • 2012-10-10
  • 2019-07-15
  • 1970-01-01
  • 2019-05-26
相关资源
最近更新 更多