【问题标题】:How to place an Image on top of other Image in React Native?如何在 React Native 中将图像放置在其他图像之上?
【发布时间】:2016-02-19 01:32:49
【问题描述】:

我放置了一个图像作为根节点,以使其成为我的视图的背景。但似乎所有其他图像都变得不可见...... 有没有办法使用内置组件将图像放置在背景之上,而不需要任何插件?
在下面的代码示例中,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'
      }
    });

【问题讨论】:

    标签: ios image react-native


    【解决方案1】:

    我认为您最好将内容包装在absolutely 定位的元素中,然后将其拉伸以覆盖屏幕,而不是将您的内容包装在&lt;Image&gt; 中。

    <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'
      }
    });
    

    【讨论】:

      【解决方案2】:

      我有同样的问题,我使用了&lt;ImageBackground&gt;

      <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
        

        这将帮助您添加带有子图像叠加层的背景图像,并且对我来说非常好。

        【讨论】:

        • 我想使用图像作为标题,并且我必须放回也是图像的图标,所以我尝试将标题图像放在 中。但是当我给它一个 100% 的宽度时很好,但我也想给它一个屏幕的 40% 的高度,但不想将图像剪切到 40% 我该怎么办?
        • 可以在指定图片样式的同时调整高度、宽度。就像上面我给的高度和宽度一样 100% 你可以相应地指定你的。例如:
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-28
        • 2010-11-16
        • 2020-05-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多