简体   繁体   English

ReactNative:如何使背景图像填满应由flexbox覆盖的部分

[英]ReactNative: How to make background image fill up the portion that it should cover by flexbox

I've been trying to get an image to fill up the space it should fill with flex. 我一直在尝试获取一张图片来填充应该用flex填充的空间。 However, it only fills up to a certain portion of the space. 但是,它仅占空间的特定部分。 The structure is as follows: 结构如下:

  • mainWrapper mainWrapper
    • navBarWrapper navBarWrapper
    • image 图片
    • buttonWrapper buttonWrapper

mainWrapper is a View with flex: 1 so it covers the entire page. mainWrapper是一个带有flex:1的视图,因此它覆盖了整个页面。 navBarWrapper has a flex: 1 image has a flex: 7 and backgroundColor: 'pink' to see where it ends buttonWrapper has a flex: 2.5 and backgroundColor: 'lightgrey' to see where it ends navBarWrapper具有flex:1图像具有flex:7和backgroundColor:'粉红色'以查看其结束位置buttonWrapper具有flex:2.5和backgroundColor:'lightgrey'以查看其终止位置

In the screenshot, the image itself doesn't fill up the pink space entirely. 在屏幕截图中,图像本身并未完全填满粉红色空间。

Screenshot 截图

My simplified code is here: 我的简化代码在这里:

  <View style={styles.mainWrapper}>
    <View style={styles.navBarWrapper}>
      <NavigationBar
        statusBar={this.statusBarConfig()}
        leftButton={this.leftButtonConfig()}
        title={this.titleConfig()}
        style={styles.navBar}>
      </NavigationBar>
    </View>

    <View style={styles.imageWrapper}>
      <Image source={require('../assets/images/user_screen/user_screen_background.png')} style={styles.image}>
      </Image>
    </View>

    <View style={styles.buttonWrapper}>
    </View>

  </View>

And the stylesheet 还有样式表

mainWrapper:{
 flex: 1,
 flexDirection: 'column',
 justifyContent: 'flex-start',
 backgroundColor: 'white'
},
navBarWrapper:{
 flex: 1,
},
navBar:{
 flexDirection: 'row',
 alignItems: 'center',
 backgroundColor: "#1ca99e",
},
imageWrapper:{
 flex: 7,
 flexDirection: 'column',
 backgroundColor: 'pink',
},
image:{
 flex:1 ,
 height: null,
 width: null,
 resizeMode: 'cover'
},
buttonWrapper:{
 flex: 2.5,
 flexDirection: 'row',
 backgroundColor: 'lightgrey'
}

Give subcontainers flex values with respect to wrapping container. 给子容器关于包装容器的柔韧性值。 Since by default flexDirection is column when you give flex 1 outer container it will take up full height of the screen. 由于默认情况下,flexDirection是column,当​​您给flex 1外容器时,它将占据整个屏幕的高度。

Assuming you need top 10% for navbar, bottom button container 25% of screen height, that gives us 65% of height for the imageWrapper. 假设您需要导航栏顶部的10%,底部按钮容器的25%的屏幕高度,这给了imageWrapper 65%的高度。

mainWrapper:{
 flex: 1,
 flexDirection: 'column',
 justifyContent: 'flex-start',
 backgroundColor: 'white'
},
navBarWrapper:{
 flex: 0.1,
},
navBar:{
 flexDirection: 'row',
 alignItems: 'center',
 backgroundColor: "#1ca99e",
},
imageWrapper:{
 flex: 0.65,
 flexDirection: 'column',
 backgroundColor: 'pink',
},
image:{
 flex:1 ,
 height: null,
 width: null,
 resizeMode: 'cover'
},
buttonWrapper:{
 flex: 0.25,
 flexDirection: 'row',
 backgroundColor: 'lightgrey'
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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