React Native中如何将一张图片置于另一张图片之上?

26

我在视图中将一个图片作为根节点以使其成为背景。但是其他所有的图片都变得不可见了... 有没有一种方式可以在不使用插件的情况下,使用内置组件将图片放在背景上面?
在以下代码示例中,landing-background被用作背景,我的logo图片是可见的,但仅在去除背景时才会显示。Text显示在背景上方而没有任何问题...

    <View style={styles.container}>
            <Image source = {require('./img/landing-background.jpg')}
              resizeMode = 'cover' style = {styles.backdrop}>
              <View style = {styles.overlay}>
                <Text style = {styles.headline}>It should appear in front of the Background Image</Text>
    <Image style = {styles.logo} source = {require('./img/logo.png')} />
              </View>

              </Image>
    </View>

var styles = StyleSheet.create({
      container: {
        flex: 1,
        alignItems: 'center',
      },
      overlay: {
        opacity: 0.5,
        backgroundColor: '#000000'
      },
      logo: {
        backgroundColor: 'rgba(0,0,0,0)',
        width: 160,
        height: 52
      },
      backdrop: {
        flex:1,
        flexDirection: 'column'
      },
      headline: {
        fontSize: 18,
        textAlign: 'center',
        backgroundColor: 'rgba(0,0,0,0)',
        color: 'white'
      }
    });
3个回答

34

与其将您的内容包装在<Image>中,我认为最好将其包装在一个绝对定位的元素中,并使其伸展到整个屏幕。

<View style={styles.container}>
  <View style = {styles.backgroundContainer}>
    <Image source = {require('./img/landing-background.jpg')} resizeMode = 'cover' style = {styles.backdrop} />
  </View>
  <View style = {styles.overlay}>
    <Text style = {styles.headline}>It should appear in front of the Background Image</Text>
    <Image style = {styles.logo} source = {require('./img/logo.png')} />
  </View>
</View>

var styles = StyleSheet.create({
  backgroundContainer: {
    position: 'absolute',
    top: 0,
    bottom: 0,
    left: 0,
    right: 0,
  },
  container: {
    flex: 1,
    alignItems: 'center',
  },
  overlay: {
    opacity: 0.5,
    backgroundColor: '#000000'
  },
  logo: {
    backgroundColor: 'rgba(0,0,0,0)',
    width: 160,
    height: 52
  },
  backdrop: {
    flex:1,
    flexDirection: 'column'
  },
  headline: {
    fontSize: 18,
    textAlign: 'center',
    backgroundColor: 'black',
    color: 'white'
  }
});

9
我遇到了同样的问题,我使用了一个名为<ImageBackground>的标签。
<View style={{height: 55,width: 55,justifyContent: 'center',alignItems: 'center'}}>

   <ImageBackground  source={{uri:this.state.image1}} style={{height: 50,width: 50}}>

            <View style={{flex: 1,justifyContent: 'flex-end',alignItems: 'flex-start'}}> //here you can change position of image with justifyContent and alignItems
                 <Image
                  source={ {uri:this.state.image2}}
                  style={{width: 20 , height: 20 ,alignItems: 'center',justifyContent: 'center',borderWidth: 1,borderColor: '#fff',borderRadius: 30}} />
            </View>

    </ImageBackground>

</View>

3
<ImageBackground style={{width:100, height:100, jsutifyContent:'center}}>
<Image/> //childimage
</ImageBackground

这将帮助您添加一个带有子图层的背景图片,对于我来说完全可以正常工作。


我想使用图像作为标题,并在其上放置一个背景图标,该图标也是一张图片,因此我尝试将标题图像放入<imagebackground>中。但是当我给它一个100%的宽度时,它很好,但我还想给它屏幕高度的40%的高度,但不想将图像裁剪到40%,我该怎么办? - Manish
您可以在指定图像样式的同时调整其高度和宽度。就像上面我将高度和宽度设置为100%一样,您也可以相应地指定自己的高度和宽度。例如:<ImageBackground style={{height:100, width:100}}> <Image style={{height:40, width:40/> </ImageBackground> - Abhirup Mukherjee

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接