在我的React Native测试中,我试图在图像上呈现一个带有白色文本的块.但相反,我在我的图片上有一个黑色的方块,里面有白色的文字.不是我所期望的.如何渲染具有透明背景的文本块?

Current result

在此处输入图像描述

Render function

render: function(){
  return (
    <View style={styles.container}>
      <Image 
        style={styles.backdrop} 
        source={{uri: 'https://unsplash.com/photos/JWiMShWiF14/download'}}>
          <Text style={styles.headline}>Headline</Text>
      </Image>
    </View>
  );
)

Stylesheet function

var styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'flex-start',
    alignItems: 'center',
    backgroundColor: '#000000',
    width: 320
  },
  backdrop: {
    paddingTop: 60,
    width: 320,
    height: 120
  },
  headline: {
    fontSize: 20,
    textAlign: 'center',
    backgroundColor: 'rgba(0,0,0,0)',
    color: 'white'
  }
});

推荐答案

PLEASE NOTE: This answer is now vastly out of date. This was applicable the day React Native was open sourced back in 2015. Today this way of doing this is deprecated.

"不推荐与子项一起使用,这将是一个错误."

See the docs https://reactnative.dev/docs/images#background-image-via-nesting




您可以通过在Image中添加View来实现这一点,如下所示:

render: function(){
  return (
    <View style={styles.container}>
      <Image 
        style={styles.backdrop} 
        source={{uri: 'https://unsplash.com/photos/JWiMShWiF14/download'}}>
          <View style={styles.backdropView}>
            <Text style={styles.headline}>Headline</Text>
          </View>
      </Image>
    </View>
  );
)

Stylesheet function

var styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'flex-start',
    alignItems: 'center',
    backgroundColor: '#000000',
    width: 320
  },
  backdrop: {
    paddingTop: 60,
    width: 320,
    height: 120
  },
  backdropView: {
    height: 120,
    width: 320,
    backgroundColor: 'rgba(0,0,0,0)',
  },
  headline: {
    fontSize: 20,
    textAlign: 'center',
    backgroundColor: 'rgba(0,0,0,0)',
    color: 'white'
  }
});

React-native相关问答推荐

React Native 中的视图与 Flex 不占用相同的高度空间

将值从一个 js 文件传递​​到 react native 中的另一个 js 文件:react native

我们应该在 react-native 移动应用程序中使用哪个图标库

react-native-firebase crashlytics 未显示在 firebase 仪表板上

react native 中 componentDidMount 和 componentDidUpdate 有什么区别

你如何使 react-native react-navigation 标签栏透明

为 React Native TextInput 创建 ref 时出现 TypeScript 问题

如何使用 React Navigation 使 TabNavigator 按钮推送模态屏幕

Firebase检测用户是否存在

如何在 React Native cli 中获取 SHA-1 密钥?

从组件的 style属性中获取 CSS 属性值

如何在 React Native 中将静态图像拉伸为背景?

Fragment片段内的react-native

ScrollView visible Scroll Bar

Console.error:no permission handler detected

React Native 得到这个错误'Unrecognized operator abs'

React Native onLayout 与 React Hooks

仅链接字体的方法

使用 React Native 和 Redux 的动画状态管理

React Native + Redux 基本认证