You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ReactNative TouchableOpacity按钮不可见但可触发onPress问题求助

问题原因

你给TouchableOpacity设置样式时误用了color属性,没有为容器设置背景色:

  • color属性仅对文本类组件生效,用于控制文字颜色
  • TouchableOpacity属于容器类组件,需要通过backgroundColor属性设置填充背景色
    你的按钮当前没有背景填充,和父视图默认的白色背景重合,所以视觉上不可见,但组件的布局、交互逻辑都正常,因此可以正常触发onPress回调。

修复方案

将样式表中button的color属性改为backgroundColor即可:

const styles = StyleSheet.create({
  button: {
      backgroundColor: "tomato",
      borderRadius: 25,
      justifyContent: "center",
      alignItems: "center",
      padding: 15,
      height: 50,
      width: "100%",
  },
  text: {
      color: "#fff",
      fontSize: 18,
      fontWeight: "bold",
      textTransform: "uppercase",
  }
})

内容的提问来源于stack exchange,提问作者Levi Walsh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 13:24:10