【问题标题】:ImageBackground not working when i call source from state当我从状态调用源时,ImageBackground 不起作用
【发布时间】:2019-04-02 10:45:11
【问题描述】:

当我从状态调用源时,ImageBackground 不显示图像。

我尝试使用条件语句,例如起初可触摸的不透明度,但不能再次工作。

state = {
    color1: "../images/yellow-orb.png",
    color2: "../images/yellow-orb.png",
}
<TouchableOpacity onPress={() => this.questSelect(1)}>
          {this.state.color1 ? (
            <ImageBackground
              source={{ uri: this.state.color1 }}
              style={styles.icon2}
            >
              <Text>1</Text>
            </ImageBackground>
          ) : (
            null
          )}
</TouchableOpacity>
<TouchableOpacity onPress={() => this.questSelect(2)}>
          <ImageBackground
            source={{ uri: this.state.color2 }}
            style={styles.icon2}
          >
            <Text>2</Text>
          </ImageBackground>
</TouchableOpacity>

在模拟器中,我可以看到像 1 2 3 .. 这样的文本,但没有图像。

【问题讨论】:

  • 你能分享一下你的 ImageBackground 的样式吗?
  • icon2: { 高度:Dimensions.get("window").width / 9,宽度:Dimensions.get("window").width / 9,marginTop:Dimensions.get("window" ).width / 60 },

标签: react-native touchableopacity imagebackground


【解决方案1】:

您需要添加require() 才能从源加载。

例如:

 <ImageBackground
              source={require("../images/yellow-orb.png")}
              style={styles.icon2}
            >
              <Text>1</Text>
 </ImageBackground>

像这样更新你的状态,

this.state = {
    color1: require("../images/yellow-orb.png"),
    color2: require("../images/yellow-orb.png"),
}

直接使用状态值,

 <ImageBackground
              source={this.state.color1}
              style={styles.icon2}
            >

请检查

【讨论】:

  • 请接受并投票,这将有助于社区改进
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-02-26
  • 2020-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多