简体   繁体   中英

How to vertically and horizontally align child components 'on top' of parent in React Native?

I'm having a lot of trouble getting two child components to vertically and horizontally align 'on top' of their parent element in React Native.

I am trying to place Loader and Button on top of RTCView .

I currently have this JSX returning in the parent:

  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>
    );
  }

And this inside 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',
  }
});

Whilst I have achieved getting Loader to display 'on top' of it's parent, I cannot move them to their position at the top of the screen. 在此处输入图片说明

Any help would be much appreciated!

Your absolute positioned View needs to cover its parent. You can achieve this by adding top: 0, left: 0, right: 0, bottom: 0 to its style

You may need to edit your Button style. I removed it from your code, add it if you need margin or a specific width.

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM