React-native:如何在Layout(View)的背景中设置图像

Moh*_*zad 8 layout background-image reactjs react-native

现在我正在使用下面的代码设置背景图像,其中创建一个额外的图像视图以在视图上设置图像并且其工作正常.

<View>
<Image 
  style={{
     height:67 ,
     width: width} 
  source={Images.header_bachkground}/>
</View>
Run Code Online (Sandbox Code Playgroud)

现在我的问题是:有没有办法直接在视图上设置背景图像,如:

<View 
  style={{
     height:67 ,
     width: width} 
  source={Images.header_bachkground}>
</View>
Run Code Online (Sandbox Code Playgroud)

上面的代码对我不起作用.

Har*_*tne 8

使用<Image />作为父组件来嵌套子元素已被弃用,很快就会抛出错误(在撰写本文时).请<ImageBackground />改用.你可以在ImageBackground中嵌套任何东西.您传递给的所有道具<Image />都可以传递给它.

请参阅.


arj*_*jun 0

您无法为视图设置图像背景。尝试添加<Image>为所有内容视图的父级,如下所示

 <Image source={require('image!background')} style={styles.container}>
    ... Your Content ...
  </Image>
Run Code Online (Sandbox Code Playgroud)

参考这个SO线程