react-native - 如何在 React Native 中将静态图像拉伸(stretch)为背景?

标签 react-native

我想在我的 native 应用程序中使用背景图片,
图像比屏幕小,所以我必须拉伸(stretch)它。

但如果图像是 则不起作用从 Assets 包加载

var styles = StyleSheet.create({
  bgImage: {
    flex: 1,
    flexDirection: 'row',
    justifyContent: 'center',
    alignItems: 'stretch',
    resizeMode: 'stretch',
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  }
});

<Image source={require('image!background')} style={styles.bgImage}>
  <Text style={styles.welcome}>
    Welcome to React Native!
  </Text>
</Image>

它看起来像这样:

enter image description here

但是,它适用于远程图像,通过 source={{uri: 'background-image-uri'}} :

enter image description here

最佳答案

对我来说使用 undefined而不是 null值(value)。在 typescript 环境下,会提示 not assignable

 bgImage: {
flex: 1,
width: undefined,
height: undefined,
resizeMode: 'stretch',
},

关于react-native - 如何在 React Native 中将静态图像拉伸(stretch)为背景?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30273624/

相关文章:

javascript - 无法从另一个文件返回数据React Native

react-native - 使用 axios 使用 Rest api

android - 如何在 Google Play 上的同一应用程序中使用另一个 keystore

react-native - react native : Allow only a certain screen to change orientation

reactjs - 每次选择/取消选择一行时,如何避免 react 原生 FlatList 重新渲染

javascript - React Native 组件样式的最佳实践

javascript - 一个月内的小时数的数学函数

reactjs - 在react-native中控制可触摸区域

javascript - 类型错误 : fetch is not a function in React Native & Jest

ios - React Native 网络请求获取失败 - Works Local