React Native - StyleSheet.absoluteFill() 和 StyleSheet.absoluteFillObject() 有什么区别?

Pur*_*tes 14 css-position absolute reactjs react-native

文档提供了一个示例,StyleSheet.absoluteFillObject()其行为在使用 with 时也相同StyleSheet.absoluteFill()

const styles = StyleSheet.create({
  wrapper: {
    ...StyleSheet.absoluteFillObject,
    top: 10,
    backgroundColor: 'transparent',
  },
});
Run Code Online (Sandbox Code Playgroud)

StyleSheet.absoluteFill()和 和有StyleSheet.absoluteFillObject()什么区别?一个小例子将更受欢迎。谢谢 !!!

Cha*_*wen 15

absoluteFill 是一种将视图设置为全屏和绝对定位的简单方法。这是一个捷径:

{
 position: 'absolute',
 top: 0,
 left: 0,
 bottom: 0,
 right: 0
Run Code Online (Sandbox Code Playgroud)

}

使用它来扩展您的其他样式,如下所示:

const styles = StyleSheet.create({
  container: {
   backgroundColor: 'red'
   }
});
<View style={[StyleSheet.absoluteFill, styles.container]} />
Run Code Online (Sandbox Code Playgroud)

absoluteFillObject 假设您想绝对定位您的视图,但将其向下移动 20px 以偏移状态栏(例如)。您可以将 StyleSheet.absoluteFillObject 扩展到您的样式中,然后覆盖其中的一个值。

const styles = StyleSheet.create({
  container: {
    ...StyleSheet.absoluteFillObject,
   top: 20,
    backgroundColor: 'red'
  }
});
<View style={styles.container} />
Run Code Online (Sandbox Code Playgroud)

  • 您在“StyleSheet.absoluteFillObject”的答案中提到的内容也可以使用“StyleSheet.absoluteFill”来完成,那么,有什么区别呢? (9认同)

Gin*_*ama 10

这两者没有区别。你可以在 StyleSheet.js 中看到这个 在此处输入图片说明

  • 是的,“使用源头,卢克”!这是检查最新版本情况的参考:https://github.com/facebook/react-native/blob/master/Libraries/StyleSheet/StyleSheet.js#L266 (3认同)

小智 7

我参加聚会可能会迟到。absoluteFill但 typescript和in之间存在一些差异absoluteFillObject

主要以typescript为主,类型为:

  • absoluteFillRegisteredStyle<StyleSheet.AbsoluteFillStyle>
  • absoluteFillObjectStyleSheet.AbsoluteFillStyle
const styles = StyleSheet.create({
    container: {
        // must use "absoluteFillObject" in typescript
        ...StyleSheet.absoluteFillObject,
    }
})
Run Code Online (Sandbox Code Playgroud)

对于 JavaScript 来说,没有区别。