在React Native View中使用paddingVertical时,未计算视图的高度

时间:2018-04-24 10:36:31

标签: react-native

这是我的渲染方法

  render() {
    return (<View style={{
      display: "flex",
      backgroundColor: "green",
      paddingVertical: 20
    }}>
      <View style={{ backgroundColor: "red", flex: 1, position: "absolute", height: "100%", width: "100%" }} />
      <Text>This is the text</Text>
      <Text>This is the text</Text>
      <Text>This is the text</Text>
      <Text>This is the text</Text>
    </View>)
  }

输出如下:

enter image description here 我期待红色视图与父绿色视图完全重叠。如果Parent没有定义paddingVertical,则效果很好。有什么方法可以解决这个问题吗?

感谢。

1 个答案:

答案 0 :(得分:0)

Red View component

height: 100% and width: 100%更改为top: 0, bottom: 0, left: 0 and right: 0

return (
  <View
    style={{
      display: "flex",
      backgroundColor: "green",
      paddingVertical: 20
    }}
  >
    <View
      style={{
        backgroundColor: "red",
        flex: 1,
        position: "absolute",
        top: 0,
        bottom: 0,
        left: 0,
        right: 0
      }}
    />
    <Text>This is the text</Text>
    <Text>This is the text</Text>
    <Text>This is the text</Text>
    <Text>This is the text</Text>
  </View>
);