将图像大小调整为视图 - 反应原生

时间:2017-07-03 10:18:36

标签: android css reactjs react-native resize

我想知道是否可以调整图像大小以使其适合视图。

这就是我现在所拥有的

 <View style={styles.container}>
    <View style={styles.header}>
      <Image
        source={headerImage}    
      />

    </View>
    <Text style={styles.basicText}>text</Text>
  </View>
 const styles = StyleSheet.create({
      container: {
        flex: 1,
        backgroundColor: "#fff"
      },
      header: {
        height: 200, 
        backgroundColor: 'red'
      },
      background: {
        width: null,
        height: null
      },
      image: {

      }

我想让图片适合标题视图。

3 个答案:

答案 0 :(得分:2)

添加到Moti Azu的答案,您还需要设置resizeMode。

image: {
  flex:1,
  height: null,
  resizeMode: 'contain',
  width: null,
}

如果似乎不起作用,请确保风格为View的{​​{1}}有宽度。

答案 1 :(得分:0)

将flex:1添加到图像中,它会在整个容器上展开。

 image: {
     flex: 1
 }

答案 2 :(得分:0)

尝试以下风格

<View style={styles.container}>
  <View style={styles.header}>
    <Image  style={styles.image}
      source={headerImage}    
    />

   </View>
  <Text style={styles.basicText}>text</Text>
</View>

样式

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: "#fff"
  },
  header: {
    height: 200, 
    backgroundColor: 'red'
  },
  background: {
    width: null,
    height: null
  },
  image: {
    width: 50,
    height: 50
  }
})

参考react native image属性

http://facebook.github.io/react-native/releases/0.42/docs/image.html#props

或在图片道具中使用resizeMode