React-Native Image Opacity为文本

时间:2017-05-18 16:16:45

标签: react-native react-native-android react-native-ios

我使用图像作为基本上是一个视图并将文本放入其中,我试图给图像赋予不透明度,但每次我尝试文本都会变得不透明我也不知道为什么会发生这种情况

enter image description here

<View style={{flex: 1}}>
  <View style={{height: 180,}}>
     <Image source={{uri : this.state.fullURLImage}} style={{flex: 1,width: window.width,justifyContent:'center',alignItems:'center',opacity: 0.7}}>
       <Text style={{textAlign: 'center',fontWeight: 'bold',color:'white',fontSize:16,}}>{this.state.title}</Text>
       <Text style={{color:'white',fontSize:14,}}>{'\n'}July {this._try(this.state.day)} | {this.state.time_start} - {this.state.time_end}</Text>
     </Image>
  </View>

  <View style={{flexGrow: 1, backgroundColor: 'white',}}>
     <Text style={{textAlign:'center',color:'#1B3D6C',fontWeight:'bold',margin:10,marginTop: 40}}>{this.state.author}</Text>
     <Text style={{margin:10,textAlign: (Platform.OS === 'ios') ? 'justify' : 'left'}}>{this.state.text}</Text>

  </View>
  <Image
     source={{uri : this.state.fullURLAuthor}}
     style={{
        position: 'absolute',
        top: 180 - 64 / 2,
        height: 64,
        width: 64,
        left: (Dimensions.get('window').width - 64) / 2,
        borderRadius: 32,
     }}
  />
</View>

3 个答案:

答案 0 :(得分:5)

您需要将文字置于图片代码之外并为其提供位置

OR

使用类似{backgroundColor: 'rgba(0,0,0,0.5)'}}

的方式提供不透明度

答案 1 :(得分:0)

您可以仅使用<ImageBackground>而不是<Image>并按照以下说明应用不透明度和颜色。

                            <ImageBackground
                                style={{flex: 1,height: 110,resizeMode: 'cover',}}
                                source={imageSoure}
                                borderRadius={5}
                                resizeMode='cover'
                                imageStyle={{ opacity: 0.3,backgroundColor: 'YourFavouredColor'}}
                            >
                                <Text>
                                    {someText}
                                </Text>
                                <Text>
                                    {someOtherText}
                                </Text>
                            </ImageBackground>

答案 2 :(得分:-1)

React按照外观顺序绘制组件。

那就是说,我认为你的图像是在文本之上绘制的。

您有多种方法可以实现布局,但请按照您的代码尝试:

<View style={{flex: 1}}>
  <Image
     source={{uri : this.state.fullURLAuthor}}
     style={{
        position: 'absolute',
        top: 180 - 64 / 2,
        height: 64,
        width: 64,
        left: (Dimensions.get('window').width - 64) / 2,
        borderRadius: 32,
     }}
  />
  <View style={{height: 180,}}>
     <Image source={{uri : this.state.fullURLImage}} style={{flex: 1,width: window.width,justifyContent:'center',alignItems:'center',opacity: 0.7}}>
       <Text style={{textAlign: 'center',fontWeight: 'bold',color:'white',fontSize:16,}}>{this.state.title}</Text>
       <Text style={{color:'white',fontSize:14,}}>{'\n'}July {this._try(this.state.day)} | {this.state.time_start} - {this.state.time_end}</Text>
     </Image>
  </View>

  <View style={{flexGrow: 1, backgroundColor: 'white',}}>
     <Text style={{textAlign:'center',color:'#1B3D6C',fontWeight:'bold',margin:10,marginTop: 40}}>{this.state.author}</Text>
     <Text style={{margin:10,textAlign: (Platform.OS === 'ios') ? 'justify' : 'left'}}>{this.state.text}</Text>

  </View>
</View>