3

在我的 react-native expo 应用程序中,我有一个在所有屏幕上都占据全高和全宽的背景图像,我想在背景图像上方放置一个线性渐变,但它不起作用,图像总是出现在渐变上方,这里是编码:

    import React, { Component } from 'react';
    import { LinearGradeint } from 'expo';
    import { Text, StyleSheet, ImageBackground } from 'react-native';

    class App extends Component {

      render() {

        return(
          <View style={styles.container}>
          <LinearGradient
          colors={['#4c669f', '#3b5998', '#192f6a']}>
            <ImageBackground source={require('./images/background.jpg')} style={styles.backgroundImage}>
            <View style={{width: "100%"}}>
            <Text>
              HI
            </Text>
          </View>
           </ImageBackground>
          </LinearGradient>
          </View>
        )
      }
    }

    const styles = StyleSheet.create({
      container: {
        flex: 1,
      },
      backgroundImage: {
        height: '100%',
        width: '100%',
        flex: 1,
      },
    export default App;
4

3 回答 3

3

尝试将 LinearGradient 组件放在 ImageBackground 中。

<ImageBackground source={require('./images/background.jpg')} style={styles.backgroundImage}>
     <LinearGradient colors={['#4c669f', '#3b5998', '#192f6a']} />
          <View style={{width: "100%"}}>
          </View>
</ImageBackground>
于 2018-12-24T08:45:17.230 回答
1

在 LinearGradient 上添加绝对位置:

<LinearGradient
   colors={['#4c669f', '#3b5998', '#192f6a']}
   style={{
      position: 'absolute',
      left: 0,
      right: 0,
      top: 0,
      height: '100%'
   }}
/>
于 2019-12-20T18:03:10.147 回答
0

这对我有用:

<ImageBackground
  source={require('../../assets/img/background.png')}
  resizeMode="cover"
  style={{
    height: '100%',
    width: '100%',
    flex: 1,
  }}>
  <LinearGradient
    colors={[
      'rgba(255,180,113,0.75)',
      'rgba(255,107,11,0.75)',
      'rgba(255,180,113,0.75)',
    ]}
    style={{flex: 1, justifyContent: 'center'}}>

    {** CODE **}

  </LinearGradient>
</ImageBackground>
于 2021-08-31T10:39:09.450 回答